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

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

    You could use the excellent Umbrella library:

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

    However, they do seem to have stored Monday as the first day of the week (see the property nVentive.Umbrella.Extensions.Calendar.DefaultDateTimeCalendarExtensions.WeekBeginsOn), so that previous localized solution is a bit better. Unfortunate.

    Edit: looking closer at the question, it looks like Umbrella might actually work for that too:

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

    Although it's worth noting that if you ask for the previous Monday on a Monday, it'll give you seven days back. But this is also true if you use BeginningOfWeek, which seems like a bug :(.

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

    For Monday

    DateTime startAtMonday = DateTime.Now.AddDays(DayOfWeek.Monday - DateTime.Now.DayOfWeek);
    

    For Sunday

    DateTime startAtSunday = DateTime.Now.AddDays(DayOfWeek.Sunday- DateTime.Now.DayOfWeek);
    
    0 讨论(0)
  • 2020-11-22 13:33

    Tried several but did not solve the issue with a week starting on a Monday, resulting in giving me the coming Monday on a Sunday. So I modified it a bit and got it working with this code:

    int delta = DayOfWeek.Monday - DateTime.Now.DayOfWeek;
    DateTime monday = DateTime.Now.AddDays(delta == 1 ? -6 : delta);
    return monday;
    
    0 讨论(0)
  • 2020-11-22 13:34
        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();
                }
            }
        } 
    
    0 讨论(0)
  • 2020-11-22 13:35

    Step 1: Create a static class

      public static class TIMEE
    {
        public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
            return dt.AddDays(-1 * diff).Date;
        }
        public static DateTime EndOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = (7 - (dt.DayOfWeek - startOfWeek)) % 7;
            return dt.AddDays(1 * diff).Date;
        }
    }
    

    Step2: Use this class to get both start and end day of the week

     DateTime dt =TIMEE.StartOfWeek(DateTime.Now ,DayOfWeek.Monday);
            DateTime dt1 = TIMEE.EndOfWeek(DateTime.Now, DayOfWeek.Sunday);
    
    0 讨论(0)
  • This would give you midnight on the first Sunday of the week:

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

    This gives you the first Monday at midnight:

    DateTime t = DateTime.Now;
    t -= new TimeSpan ((int) t.DayOfWeek - 1, t.Hour, t.Minute, t.Second);
    
    0 讨论(0)
提交回复
热议问题