使用扩展方法.你知道,他们是一切的答案!;)
public static class DateTimeExtensions { public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7; return dt.AddDays(-1 * diff).Date; } }
可以使用如下:
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday); DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
我能想出的最快方法是:
var sunday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek);
如果您希望将一周中的任何其他日期作为开始日期,那么您需要做的就是将DayOfWeek值添加到最后
var monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday); var tuesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Tuesday);
更加冗长和文化意识:
System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek; DayOfWeek today = DateTime.Now.DayOfWeek; DateTime sow = DateTime.Now.AddDays(-(today - fdow)).Date;
使用Fluent DateTime:
var monday = DateTime.Now.Previous(DayOfWeek.Monday); var sunday = DateTime.Now.Previous(DayOfWeek.Sunday);
丑陋但它至少给出了正确的日期
按系统设置的周开始:
public static DateTime FirstDateInWeek(this DateTime dt) { while (dt.DayOfWeek != System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek) dt = dt.AddDays(-1); return dt; }
无:
public static DateTime FirstDateInWeek(this DateTime dt, DayOfWeek weekStartDay) { while (dt.DayOfWeek != weekStartDay) dt = dt.AddDays(-1); return dt; }
让我们结合文化安全答案和扩展方法答案:
public static class DateTimeExtensions { public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek; return DateTime.Today.AddDays(-(DateTime.Today.DayOfWeek- fdow)); } }
这可能是一个黑客,但你可以将.DayOfWeek属性转换为int(它是一个枚举,因为它的基础数据类型没有更改,默认为int)并使用它来确定一周的前一个开始.
看起来DayOfWeek枚举中指定的星期在星期日开始,所以如果我们从这个值中减去1,它将等于星期一在当前日期之前的天数.我们还需要将星期日(0)映射到等于7,因此给定1 - 7 = -6星期日将映射到上一个星期一: -
DateTime now = DateTime.Now; int dayOfWeek = (int)now.DayOfWeek; dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek; DateTime startOfWeek = now.AddDays(1 - (int)now.DayOfWeek);
上周日的代码更简单,因为我们无需进行此调整: -
DateTime now = DateTime.Now; int dayOfWeek = (int)now.DayOfWeek; DateTime startOfWeek = now.AddDays(-(int)now.DayOfWeek);
这会给你前一个星期天(我想):
DateTime t = DateTime.Now; t -= new TimeSpan ((int) t.DayOfWeek, 0, 0, 0);
对于星期一
DateTime startAtMonday = DateTime.Now.AddDays(DayOfWeek.Monday - DateTime.Now.DayOfWeek);
周日
DateTime startAtSunday = DateTime.Now.AddDays(DayOfWeek.Sunday- DateTime.Now.DayOfWeek);
using System; using System.Globalization; namespace MySpace { public static class DateTimeExtention { // ToDo: Need to provide culturaly neutral versions. public static DateTime GetStartOfWeek(this DateTime dt) { DateTime ndt = dt.Subtract(TimeSpan.FromDays((int)dt.DayOfWeek)); return new DateTime(ndt.Year, ndt.Month, ndt.Day, 0, 0, 0, 0); } public static DateTime GetEndOfWeek(this DateTime dt) { DateTime ndt = dt.GetStartOfWeek().AddDays(6); return new DateTime(ndt.Year, ndt.Month, ndt.Day, 23, 59, 59, 999); } public static DateTime GetStartOfWeek(this DateTime dt, int year, int week) { DateTime dayInWeek = new DateTime(year, 1, 1).AddDays((week - 1) * 7); return dayInWeek.GetStartOfWeek(); } public static DateTime GetEndOfWeek(this DateTime dt, int year, int week) { DateTime dayInWeek = new DateTime(year, 1, 1).AddDays((week - 1) * 7); return dayInWeek.GetEndOfWeek(); } } }