據當前時間獲取本學期周次

2021-05-31 22:55:21 字數 1001 閱讀 1578

已經設定好了本學期第乙個星期星期一的日期,怎樣根據當前時間獲取獲取周次呢

datetime dtime=datetime.now;

string datetime = "2011-3-1";//本學期第乙個星期一的日期

timespan ts1 = new timespan(dtime.ticks);

timespan ts2 = new timespan(convert.todatetime(datetime).ticks);

timespan ts = ts2.subtract(ts1).duration();

//確定此時間在一年中的位置

int dayofyear = ts.days;

//本學期第一天

datetime tempdate = new datetime(convert.todatetime(datetime).year, convert.todatetime(datetime).month, convert.todatetime(datetime).day);

int tempdayofweek = (int)tempdate.dayofweek;

tempdayofweek = tempdayofweek == 0 ? 7 : tempdayofweek;

確定星期幾

int index = (int)dtime.dayofweek;

index = index == 0 ? 7 : index;

//當前周的範圍

datetime retstartday = dtime.adddays(-(index - 1));

datetime retendday = dtime.adddays(7 - index);

//確定當前是第幾周

int weekindex = (int)math.ceiling(((double)dayofyear + tempdayofweek) / 7);

response.write("當前是第"+weekindex+"周");

Java獲取當前時間是週幾

1 使用calendar類 獲取當前日期是星期幾 param dt return 當前日期是星期幾 public static string getweekofdate date dt calendar cal calendar.getinstance cal.settime dt int w ca...

獲取時間段,遍歷學期,周時間查詢周排名第一學生

靜態方法 獲取當前月開始與結束時間 return public static map getmonth 獲取當前學期 去年9月 今年2月上半學期,今年3 8下半學期 param contains return map public static map getquarter boolean cont...

獲取當前上一周 上一月 上一年的時間

dateformat format new dateformat yyyy mm dd hh mm ss calendar c calendar.getinstance 1.過去七天 c.settime new date c.add calendar.date,7 date d c.gettime ...