我怎样才能获得一周的开始date时间?

我怎样才能find一周的开始(周日和周一),只知道C#中的当前时间?

就像是:

DateTime.Now.StartWeek(Monday); 

使用扩展方法。 他们是一切的答案,你知道! ;)

 public static class DateTimeExtensions { public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { int diff = dt.DayOfWeek - startOfWeek; if (diff < 0) { diff += 7; } return dt.AddDays(-1 * diff).Date; } } 

其中可以使用如下:

 DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday); DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday); 

一点更详细和文化意识:

 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; 

我能想到的最快的方法是:

 var sunday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek); 

如果您希望每周的其他日子成为您的开始date,您只需将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); 

使用Fluent DateTime :

 var monday = DateTime.Now.Previous(DayOfWeek.Monday); var sunday = DateTime.Now.Previous(DayOfWeek.Sunday); 

让我们结合文化安全的答案和扩展方法的答案:

 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)); } } 

丑陋的,但至less给了正确的date

由系统设置的周开始:

  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; } 

这可能有点破解,但是你可以将.DayOfWeek属性强制转换为一个int(这是一个枚举,因为它的基础数据types没有改变它的默认值为int),并用它来确定本周的上一次开始。

看来DayOfWeek枚举中指定的星期从星期天开始,所以如果我们从这个值中减去1,就等于星期一在当前date之前有多less天。 我们还需要将星期天(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); 
 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(); } } } 

把它放在一起,全球化,并允许指定一周的第一天作为我们的电话的一部分

 public static DateTime StartOfWeek ( this DateTime dt, DayOfWeek? firstDayOfWeek ) { DayOfWeek fdow; if ( firstDayOfWeek.HasValue ) { fdow = firstDayOfWeek.Value; } else { System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; fdow = ci.DateTimeFormat.FirstDayOfWeek; } int diff = dt.DayOfWeek - fdow; if ( diff < 0 ) { diff += 7; } return dt.AddDays( -1 * diff ).Date; } 

这会在本周的第一个星期天午夜给你:

 DateTime t = DateTime.Now; t -= new TimeSpan ((int) t.DayOfWeek, t.Hour, t.Minute, t.Second); 

这给你午夜的第一个星期一:

 DateTime t = DateTime.Now; t -= new TimeSpan ((int) t.DayOfWeek - 1, t.Hour, t.Minute, t.Second); 
 var now = System.DateTime.Now; var result = now.AddDays(-((now.DayOfWeek - System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7)).Date; 

以下方法应该返回所需的DateTime。 星期天是星期一的第一天,星期一是假的:

 private DateTime getStartOfWeek(bool useSunday) { DateTime now = DateTime.Now; int dayOfWeek = (int)now.DayOfWeek; if(!useSunday) dayOfWeek--; if(dayOfWeek < 0) {// day of week is Sunday and we want to use Monday as the start of the week // Sunday is now the seventh day of the week dayOfWeek = 6; } return now.AddDays(-1 * (double)dayOfWeek); } 

感谢您的例子。 我需要一直使用“CurrentCulture”一周的第一天,我需要知道确切的Daynumber ..所以这里是我的第一个扩展:

 public static class DateTimeExtensions { //http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week //http://stackoverflow.com/questions/1788508/calculate-date-with-monday-as-dayofweek1 public static DateTime StartOfWeek(this DateTime dt) { //difference in days int diff = (int)dt.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; //sunday=always0, monday=always1, etc. //As a result we need to have day 0,1,2,3,4,5,6 if (diff < 0) { diff += 7; } return dt.AddDays(-1 * diff).Date; } public static int DayNoOfWeek(this DateTime dt) { //difference in days int diff = (int)dt.DayOfWeek - (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; //sunday=always0, monday=always1, etc. //As a result we need to have day 0,1,2,3,4,5,6 if (diff < 0) { diff += 7; } return diff + 1; //Make it 1..7 } } 

用这个代码在c#中试试。用这个代码,你可以得到给定星期的第一个date和最后一个date。这里是星期天是第一天,星期六是最后一天,但是你可以根据你的文化

 DateTime firstDate = GetFirstDateOfWeek(DateTime.Parse("05/09/2012").Date,DayOfWeek.Sunday); DateTime lastDate = GetLastDateOfWeek(DateTime.Parse("05/09/2012").Date, DayOfWeek.Saturday); public static DateTime GetFirstDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay) { DateTime firstDayInWeek = dayInWeek.Date; while (firstDayInWeek.DayOfWeek != firstDay) firstDayInWeek = firstDayInWeek.AddDays(-1); return firstDayInWeek; } public static DateTime GetLastDateOfWeek(DateTime dayInWeek, DayOfWeek firstDay) { DateTime lastDayInWeek = dayInWeek.Date; while (lastDayInWeek.DayOfWeek != firstDay) lastDayInWeek = lastDayInWeek.AddDays(1); return lastDayInWeek; } 

模数在C#工程不好为-1mod7(应该是6,C#返回-1)所以…“oneliner”解决scheme,这将看起来像这样:)

 private static DateTime GetFirstDayOfWeek(DateTime date) { return date.AddDays(-(((int)date.DayOfWeek - 1) - (int)Math.Floor((double)((int)date.DayOfWeek - 1) / 7) * 7)); } 

试了几个星期一开始的一个星期没有解决这个问题,导致给我一个星期天即将到来的星期一。 所以我修改了一下,并得到它与这个代码的工作:

 int delta = DayOfWeek.Monday - DateTime.Now.DayOfWeek; DateTime monday = DateTime.Now.AddDays(delta == 1 ? -6 : delta); return monday; 

你可以使用优秀的伞库 :

 using nVentive.Umbrella.Extensions.Calendar; DateTime beginning = DateTime.Now.BeginningOfWeek(); 

但是,他们似乎已将星期一存储为一周的第一天(请参阅属性nVentive.Umbrella.Extensions.Calendar.DefaultDateTimeCalendarExtensions.WeekBeginsOn ),以便以前的本地化解决scheme稍好一些。 不幸的。

编辑 :仔细看看这个问题,它看起来像伞也可能实际上工作:

 // Or DateTime.Now.PreviousDay(DayOfWeek.Monday) DateTime monday = DateTime.Now.PreviousMonday(); DateTime sunday = DateTime.Now.PreviousSunday(); 

虽然值得注意的是,如果你在星期一的前一个星期一请求,它会给你七天的时间。 但是,如果您使用BeginningOfWeek ,这似乎也是一个错误:(。

这将返回一周的开始和星期的结束date:

  private string[] GetWeekRange(DateTime dateToCheck) { string[] result = new string[2]; TimeSpan duration = new TimeSpan(0, 0, 0, 0); //One day DateTime dateRangeBegin = dateToCheck; DateTime dateRangeEnd = DateTime.Today.Add(duration); dateRangeBegin = dateToCheck.AddDays(-(int)dateToCheck.DayOfWeek); dateRangeEnd = dateToCheck.AddDays(6 - (int)dateToCheck.DayOfWeek); result[0] = dateRangeBegin.Date.ToString(); result[1] = dateRangeEnd.Date.ToString(); return result; } 

我已经发布了完整的代码来计算我的博客ZamirsBlog的周,月,季和年的开始/结束

似乎还没有人正确回答这个问题。 我会粘贴我的解决scheme,以防万一需要。 下面的代码工作,无论星期几的第一天是星期一还是星期天还是别的。

 public static class DateTimeExtension { public static DateTime GetFirstDayOfThisWeek(this DateTime d) { CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture; var first = (int)ci.DateTimeFormat.FirstDayOfWeek; var current = (int)d.DayOfWeek; var result = first <= current ? d.AddDays(-1 * (current - first)) : d.AddDays(first - current - 7); return result; } } class Program { static void Main() { System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); Console.WriteLine("Current culture set to en-US"); RunTests(); Console.WriteLine(); System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("da-DK"); Console.WriteLine("Current culture set to da-DK"); RunTests(); Console.ReadLine(); } static void RunTests() { Console.WriteLine("Today {1}: {0}", DateTime.Today.Date.GetFirstDayOfThisWeek(), DateTime.Today.Date.ToString("yyyy-MM-dd")); Console.WriteLine("Saturday 2013-03-02: {0}", new DateTime(2013, 3, 2).GetFirstDayOfThisWeek()); Console.WriteLine("Sunday 2013-03-03: {0}", new DateTime(2013, 3, 3).GetFirstDayOfThisWeek()); Console.WriteLine("Monday 2013-03-04: {0}", new DateTime(2013, 3, 4).GetFirstDayOfThisWeek()); } } 
  namespace DateTimeExample { using System; public static class DateTimeExtension { public static DateTime GetMonday(this DateTime time) { if (time.DayOfWeek != DayOfWeek.Monday) return GetMonday(time.AddDays(-1)); //Recursive call return time; } } internal class Program { private static void Main() { Console.WriteLine(DateTime.Now.GetMonday()); Console.ReadLine(); } } } 

同样的一周结束(风格@编译这是答案):

  public static DateTime EndOfWeek(this DateTime dt) { int diff = 7 - (int)dt.DayOfWeek; diff = diff == 7 ? 0 : diff; DateTime eow = dt.AddDays(diff).Date; return new DateTime(eow.Year, eow.Month, eow.Day, 23, 59, 59, 999) { }; } 
  d = DateTime.Now; int dayofweek =(int) d.DayOfWeek; if (dayofweek != 0) { d = d.AddDays(1 - dayofweek); } else { d = d.AddDays(-6); } 
 public static System.DateTime getstartweek() { System.DateTime dt = System.DateTime.Now; System.DayOfWeek dmon = System.DayOfWeek.Monday; int span = dt.DayOfWeek - dmon; dt = dt.AddDays(-span); return dt; }