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

前端 未结 30 2204
走了就别回头了
走了就别回头了 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:18

    Following on from Compile This' Answer, use the following method to obtain the date for any day of the week:

    public static DateTime GetDayOfWeek(DateTime dateTime, DayOfWeek dayOfWeek)
    {
       var monday = dateTime.Date.AddDays((7 + (dateTime.DayOfWeek - DayOfWeek.Monday) % 7) * -1);
    
       var diff = dayOfWeek - DayOfWeek.Monday;
    
       if (diff == -1)
       {
          diff = 6;
       }
    
       return monday.AddDays(diff);
    } 
    
    0 讨论(0)
  • 2020-11-22 13:19

    Thanks for the examples. I needed to always use the "CurrentCulture" first day of the week and for an array I needed to know the exact Daynumber.. so here are my first extensions:

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

    Modulo in C# works bad for -1mod7 (should be 6, c# returns -1) so... "oneliner" solution to this will look like this :)

    private static DateTime GetFirstDayOfWeek(DateTime date)
        {
            return date.AddDays(-(((int)date.DayOfWeek - 1) - (int)Math.Floor((double)((int)date.DayOfWeek - 1) / 7) * 7));
        }
    
    0 讨论(0)
  • 2020-11-22 13:19

    Calculating this way lets you choose which day of the week indicates the start of a new week (in the example I chose Monday).

    Note that doing this calculation for a day that is a Monday will give the current Monday and not the previous one.

    //Replace with whatever input date you want
    DateTime inputDate = DateTime.Now;
    
    //For this example, weeks start on Monday
    int startOfWeek = (int)DayOfWeek.Monday;
    
    //Calculate the number of days it has been since the start of the week
    int daysSinceStartOfWeek = ((int)inputDate.DayOfWeek + 7 - startOfWeek) % 7;
    
    DateTime previousStartOfWeek = inputDate.AddDays(-daysSinceStartOfWeek);
    
    0 讨论(0)
  • 2020-11-22 13:20

    We like one-liners : Get the difference between the current culture's first day of week and the current day then subtract the number of days from the current day

    var weekStartDate = DateTime.Now.AddDays(-((int)now.DayOfWeek - (int)DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek));
    
    0 讨论(0)
  • 2020-11-22 13:22

    Ugly but it at least gives the right dates back

    With start of week set by system:

        public static DateTime FirstDateInWeek(this DateTime dt)
        {
            while (dt.DayOfWeek != System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
                dt = dt.AddDays(-1);
            return dt;
        }
    

    Without:

        public static DateTime FirstDateInWeek(this DateTime dt, DayOfWeek weekStartDay)
        {
            while (dt.DayOfWeek != weekStartDay)
                dt = dt.AddDays(-1);
            return dt;
        }
    
    0 讨论(0)
提交回复
热议问题