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

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

    Same for end of week (in style of @Compile This's answer):

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

    This may be a bit of a hack, but you can cast the .DayOfWeek property to an int (it's an enum and since its not had its underlying data type changed it defaults to int) and use that to determine the previous start of the week.

    It appears the week specified in the DayOfWeek enum starts on Sunday, so if we subtract 1 from this value that'll be equal to how many days the Monday is before the current date. We also need to map the Sunday (0) to equal 7 so given 1 - 7 = -6 the Sunday will map to the previous Monday:-

    DateTime now = DateTime.Now;
    int dayOfWeek = (int)now.DayOfWeek;
    dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
    DateTime startOfWeek = now.AddDays(1 - (int)now.DayOfWeek);
    

    The code for the previous Sunday is simpler as we don't have to make this adjustment:-

    DateTime now = DateTime.Now;
    int dayOfWeek = (int)now.DayOfWeek;
    DateTime startOfWeek = now.AddDays(-(int)now.DayOfWeek);
    
    0 讨论(0)
  • 2020-11-22 13:23
    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:25

    Putting it all together, with Globalization and allowing for specifying the first day of the week as part of the call we have

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

    if you want saturday or sunday or any day of week but not exceeding current week(Sat-Sun) I got you covered with this piece of code.

    public static DateTime GetDateInCurrentWeek(this DateTime date, DayOfWeek day)
    {
        var temp = date;
        var limit = (int)date.DayOfWeek;
        var returnDate = DateTime.MinValue;
    
        if (date.DayOfWeek == day) return date;
    
        for (int i = limit; i < 6; i++)
        {
            temp = temp.AddDays(1);
    
            if (day == temp.DayOfWeek)
            {
                returnDate = temp;
                break;
            }
        }
        if (returnDate == DateTime.MinValue)
        {
            for (int i = limit; i > -1; i++)
            {
                date = date.AddDays(-1);
    
                if (day == date.DayOfWeek)
                {
                    returnDate = date;
                    break;
                }
            }
        }
        return returnDate;
    }
    
    0 讨论(0)
  • 2020-11-22 13:27

    A little more verbose and culture-aware:

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