date时间 – 下周二

我怎样才能得到下星期二的date?

在PHP中,它就像strtotime('next tuesday');一样简单strtotime('next tuesday');

我怎样才能在.NET中实现类似的东西

正如我在评论中提到的那样,“下个星期二”可能有不同的意思,但是这个代码给你“下个星期二发生,或者今天如果已经是星期二”:

 DateTime today = DateTime.Today; // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysUntilTuesday = ((int) DayOfWeek.Tuesday - (int) today.DayOfWeek + 7) % 7; DateTime nextTuesday = today.AddDays(daysUntilTuesday); 

如果你想给“一个星期的时间”,如果已经是星期二了,你可以使用:

 // This finds the next Monday (or today if it's Monday) and then adds a day... so the // result is in the range [1-7] int daysUntilTuesday = (((int) DayOfWeek.Monday - (int) today.DayOfWeek + 7) % 7) + 1; 

…或者你可以使用原来的公式,但从明天起:

 DateTime tomorrow = DateTime.Today.AddDays(1); // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysUntilTuesday = ((int) DayOfWeek.Tuesday - (int) tomorrow.DayOfWeek + 7) % 7; DateTime nextTuesday = tomorrow.AddDays(daysUntilTuesday); 

编辑:只是为了使这个很好,多才多艺:

 public static DateTime GetNextWeekday(DateTime start, DayOfWeek day) { // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysToAdd = ((int) day - (int) start.DayOfWeek + 7) % 7; return start.AddDays(daysToAdd); } 

所以要获得“今天或未来六天”的价值:

 DateTime nextTuesday = GetNextWeekday(DateTime.Today, DayOfWeek.Tuesday); 

要获得“下个星期二不包括今天”的价值:

 DateTime nextTuesday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Tuesday); 

这应该做的伎俩:

 static DateTime GetNextWeekday(DayOfWeek day) { DateTime result = DateTime.Now.AddDays(1); while( result.DayOfWeek != day ) result = result.AddDays(1); return result; } 
 DateTime nextTuesday = DateTime.Today.AddDays(((int)DateTime.Today.DayOfWeek - (int)DayOfWeek.Tuesday) + 7); 

这个问题没有详细的,更聪明的/优雅的解决scheme,但是下面的C#函数在很多情况下工作得很好。

 /// <summary> /// Find the closest weekday to the given date /// </summary> /// <param name="includeStartDate">if the supplied date is on the specified day of the week, return that date or continue to the next date</param> /// <param name="searchForward">search forward or backward from the supplied date. if a null parameter is given, the closest weekday (ie in either direction) is returned</param> public static DateTime ClosestWeekDay(this DateTime date, DayOfWeek weekday, bool includeStartDate = true, bool? searchForward=true) { if (!searchForward.HasValue && !includeStartDate) { throw new ArgumentException("if searching in both directions, start date must be a valid result"); } var day = date.DayOfWeek; int add = ((int)weekday - (int)day); if (searchForward.HasValue) { if (add < 0 && searchForward.Value) { add += 7; } else if (add > 0 && !searchForward.Value) { add -= 7; } else if (add == 0 && !includeStartDate) { add = searchForward.Value ? 7 : -7; } } else if (add < -3) { add += 7; } else if (add > 3) { add -= 7; } return date.AddDays(add); } 
 DateTime nexttuesday=DateTime.Today.AddDays(1); while(nexttuesday.DayOfWeek!=DayOfWeek.Tuesday) nexttuesday = nexttuesday.AddDays(1); 

@Jon Skeet很好的回答。

前一天:

 private DateTime GetPrevWeekday(DateTime start, DayOfWeek day) { // The (... - 7) % 7 ensures we end up with a value in the range [0, 6] int daysToRemove = ((int) day - (int) start.DayOfWeek - 7) % 7; return start.AddDays(daysToRemove); } 

谢谢!!

现在在oneliner的味道 – 如果你需要将它作为parameter passing到某种机制。

 DateTime.Now.AddDays(((int)yourDate.DayOfWeek - (int)DateTime.Now.DayOfWeek + 7) % 7).Day 

在这个特定的情况下:

 DateTime.Now.AddDays(((int)DayOfWeek.Tuesday - (int)DateTime.Now.DayOfWeek + 7) % 7).Day 

也可以是一个扩展,这一切都取决于

 public static class DateTimeExtensions { public static IEnumerable<DateTime> Next(this DateTime date, DayOfWeek day) { // This loop feels expensive and useless, but the point is IEnumerable while(true) { if (date.DayOfWeek == day) { yield return date; } date = date.AddDays(1); } } } 

用法

  var today = DateTime.Today; foreach(var monday in today.Next(DayOfWeek.Monday)) { Console.WriteLine(monday); Console.ReadKey(); } 

Objective C版本:

 +(NSInteger) daysUntilNextWeekday: (NSDate*)startDate withTargetWeekday: (NSInteger) targetWeekday { NSInteger startWeekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:startDate]; return (targetWeekday - startWeekday + 7) % 7; }