How can I get the DateTime for the start of the week?

前端 未结 30 2202
走了就别回头了
走了就别回头了 2020-11-22 12:57

How do I find the start of the week (both Sunday and Monday) knowing just the current time in C#?

Something like:

DateTime.Now.StartWeek(Monday);


        
相关标签:
30条回答
  • 2020-11-22 13:10

    Quickest way I can come up with is:

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

    If you would like any other day of the week to be your start date all you need to do is add the DayOfWeek value to the end

    var monday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Monday);
    
    var tuesday = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + (int)DayOfWeek.Tuesday); 
    
    0 讨论(0)
  • 2020-11-22 13:14
    var now = System.DateTime.Now;
    
    var result = now.AddDays(-((now.DayOfWeek - System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 7) % 7)).Date;
    
    0 讨论(0)
  • 2020-11-22 13:14

    This will return both the beginning of the week and the end of the week dates:

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

    I have posted the complete code for calculating the begin/end of week, month, quarter and year on my blog ZamirsBlog

    0 讨论(0)
  • 2020-11-22 13:15

    Let's combine the culture-safe answer and the extension method answer:

    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));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:18

    Use an extension method. They're the answer to everything, you know! ;)

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

    Which can be used as follows:

    DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
    DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
    
    0 讨论(0)
  • 2020-11-22 13:18

    No one seems to have answered this correctly yet. I'll paste my solution here in case anyone needs it. The following code works regardless if first day of the week is a monday or a sunday or something else.

    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());
      }
    }
    
    0 讨论(0)
提交回复
热议问题