获取给定日期的正确周数。
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;Calendar cal = dfi.Calendar;return cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
return new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
CultureInfo ciCurr = CultureInfo.CurrentCulture;int weekNum = ciCurr.Calendar.GetWeekOfYear(dtPassed, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);return weekNum;
更新
// This presumes that weeks start with Monday.// Week 1 is the 1st week of the year with a Thursday in it.public static int GetIso8601WeekOfYear(DateTime time){ // Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll // be the same week# as whatever Thursday, Friday or Saturday are, // and we always get those right DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time); if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday) { time = time.AddDays(3); } // Return the week of our adjusted day return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);}
喵喔喔
相关分类