用.NET计算一个月的星期几

.NET库有一个简单的方法来返回给定date的星期数? 例如, Year = 2010, Month = 1, Day = 25应输出5为周数。

最近我发现是Calendar.GetWeekOfYear ,几乎在那里。

Java有一个datestring格式“W”,它在一个月内返回一周,但在.NET中我看不到任何等同的东西。

没有内置的方法来做到这一点,但这里是一个扩展方法,应该为你做的工作:

 static class DateTimeExtensions { static GregorianCalendar _gc = new GregorianCalendar(); public static int GetWeekOfMonth(this DateTime time) { DateTime first = new DateTime(time.Year, time.Month, 1); return time.GetWeekOfYear() - first.GetWeekOfYear() + 1; } static int GetWeekOfYear(this DateTime time) { return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); } } 

用法:

 DateTime time = new DateTime(2010, 1, 25); Console.WriteLine(time.GetWeekOfMonth()); 

输出:

 5 

你可以根据你的需要改变GetWeekOfYear

没有直接内置的方法来做到这一点,但它可以很容易地完成。 这里是一个扩展的方法,可以用来很容易地得到一个date的年份为基础的周数:

 public static int GetWeekNumber(this DateTime date) { return GetWeekNumber(date, CultureInfo.CurrentCulture); } public static int GetWeekNumber(this DateTime date, CultureInfo culture) { return culture.Calendar.GetWeekOfYear(date, culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek); } 

然后,我们可以用它来计算以月为单位的周数,就像Jason所示的那样。 文化友好的版本可能是这样的:

 public static int GetWeekNumberOfMonth(this DateTime date) { return GetWeekNumberOfMonth(date, CultureInfo.CurrentCulture); } public static int GetWeekNumberOfMonth(this DateTime date, CultureInfo culture) { return date.GetWeekNumber(culture) - new DateTime(date.Year, date.Month, 1).GetWeekNumber(culture) + 1; // Or skip +1 if you want the first week to be 0. } 

标题是误导性的。 问题实际上是关于月份的日历星期数 (如果您计算日历中的行的星期数),而不是实际的星期数。

对于当月的周数,您可以使用:

 VB: Function WeekNumber(TargetDate As Date) As Integer Return (TargetDate.Day - 1) \ 7 + 1 End Function C#: public int WeekNumber(DateTime TargetDate) { return (TargetDate.Day - 1) / 7 + 1; } 

如下所示: http : //forums.asp.net/t/1268112.aspx

您可以使用:

 public static int Iso8601WeekNumber(DateTime dt) { return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); } public static int GetWeekOfMonth(DateTime dt) { int weekOfYear = Iso8601WeekNumber(dt); if (dt.Month == 1) { //week of year == week of month in January //this also fixes the overflowing last December week return weekOfYear; } int weekOfYearAtFirst = Iso8601WeekNumber(dt.AddDays(1 - dt.Day)); return weekOfYear - weekOfYearAtFirst + 1; } 

请注意,由于有某些algorithmselect,您的需求可能会有所不同。 即一周的星期一到星期五是二月底还是三月的第一周,或者什么标准定义了一个月的“第一个星期”? ISO 8601:2004未给出“每周”的定义。

@Svish

VB版本:

 Public Shared Function GetWeekNumber(ByVal [date] As DateTime) As Integer Return GetWeekNumber([date], CultureInfo.CurrentCulture) End Function Public Shared Function GetWeekNumber(ByVal [date] As DateTime, ByVal culture As CultureInfo) As Integer Return culture.Calendar.GetWeekOfYear([date], culture.DateTimeFormat.CalendarWeekRule, culture.DateTimeFormat.FirstDayOfWeek) End Function Public Shared Function GetWeekNumberOfMonth(ByVal [date] As DateTime) As Integer Return GetWeekNumberOfMonth([date], CultureInfo.CurrentCulture) End Function Public Shared Function GetWeekNumberOfMonth(ByVal [date] As DateTime, ByVal culture As CultureInfo) As Integer Return GetWeekNumber([date], culture) - GetWeekNumber(New DateTime([date].Year, [date].Month, 1), culture) + 1 ' Or skip +1 if you want the first week to be 0. End Function 

这是我的解决scheme:

  static string GetWeekNumber() { var weekNumber = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) - CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now.AddDays(1 - DateTime.Now.Day), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) + 1; switch (weekNumber) { case 1 : return "First"; case 2: return "Second"; case 3: return "Third"; default: return "Fourth"; } } 
 public int WeekCalc(DateTime dt) { CultureInfo ciCurr = CultureInfo.CurrentCulture; int weekNum = ciCurr.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); return weekNum; } 

这是一个古老的线程,但我需要这样的东西,这就是我想出的东西。

 public static int WeekOfMonth(this DateTime date) { DateTime firstDayOfMonth = new DateTime(date.Year, date.Month, 1); int firstDay = (int)firstDayOfMonth.DayOfWeek; if (firstDay == 0) { firstDay = 7; } double d = (firstDay + date.Day - 1) / 7.0; return (int)Math.Ceiling(d); } 

注意:如果你想使用星期一作为一周的第一天,这应该工作。 如果您希望这个星期在星期天开始,那么在2015年3月的第二个星期以及其他date可能会失败。 2015年3月9日(星期一)是第二周的第一天。 上面的计算在这个date返回“3”。

一旦你有了一个月的第一天的DayOfWeek,你可以使用整数算术得到一个月的一周。

 static class DateTimeExtensions { // Assumes that in current culture week starts on Sunday public static int GetWeekOfMonth(this DateTimeOffset time) { DateTimeOffset first = new DateTimeOffset(time.Year, time.Month, 1, 0, 0, 0, time.Offset); int firstSunday = (7 - (int)first.DayOfWeek) % 7 + 1; int weekOfMonth = 1 + (time.Day + 7 - firstSunday) / 7; return weekOfMonth; } } 

一些简单的… 🙂

 private int WeeksNumber() { int daysInMonth = DateTime.DaysInMonth(YourYear, YourMonth); DateTime date = new DateTime(Year, Month, 1); switch (daysInMonth) { case 28: { if (date.DayOfWeek == DayOfWeek.Monday) { return 4; } else { return 5; } } case 29: { return 5; } case 30: { if (date.DayOfWeek == DayOfWeek.Sunday) { return 6; } else { return 5; } } case 31: { if (date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday) { return 6; } else { return 5; } } default: return 5; } } 

如果您希望将前7天视为第一周,即使该月份并非在星期天开始,也是一个简单的选项:

 class Program { static void Main(string[] args) { var dates = Enumerable.Range(0, 10) .Select(r => new DateTime(2015, 10, 1).AddDays(r)) .Select(q => new { Date = q, WeekOfMonth = q.WeekOfMonth() }); foreach(var item in dates) { Console.WriteLine("Date: {0:ddd MM-dd-yyyy}, WOM: {1}", item.Date, item.WeekOfMonth); } var final = Console.ReadLine(); } } public static class DateTimeExtensions { public static int WeekOfMonth(this DateTime d) { var isExact = (d.Day % 7 == 0); var offset = isExact ? 0 : 1; return (int)Math.Floor(d.Day / 7.0) + offset; } } 
  Dim mdt As Date = txtDT.Text Dim weekNum As Integer = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(mdt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) - CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(GetFirstDayOfMonth(mdt), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday) + 1 Response.Write(weekNum)