Wednesday, June 22, 2011

Last and first day of the week

In a project I had to find out if a specific date was the first or last day of the week. Also, here in Norway we have Monday as the first day of the week, but in USA they use Sunday. I wanted my code to support this as well.



My first task was to find the first day of the week, and I wanted to add it to a DateTime utility class:
1:  using System;  
2:  using System.Globalization;  
3:    
4:  namespace MyProject.Utils  
5:  {  
6:    /// <summary>  
7:    /// Represents a <see cref="System.DateTime"/> utility class.  
8:    /// </summary>  
9:    public static class DateTimeUtils  
10:    {  
11:      /// <summary>  
12:      /// Gets the first day of week.  
13:      /// </summary>  
14:      /// <returns>The first day of the week</returns>  
15:      public static DayOfWeek GetFirstDayOfWeek()  
16:      {  
17:        // Gets the current culture  
18:        var currentCulture = CultureInfo.CurrentCulture;  
19:          
20:        // Gets the first day of the week.  
21:        var firstDayOfWeek = currentCulture.DateTimeFormat.FirstDayOfWeek;  
22:          
23:        return firstDayOfWeek;  
24:      }  
25:    }  
26:  }  

From here it was easy to create a method extension that can check if a specific date is the first day of the week:
1:    
2:  /// <summary>  
3:  /// Gets a value indicating whether the specified date is the first day of the week.  
4:  /// </summary>  
5:  /// <param name="value">The value.</param>  
6:  /// <returns>  
7:  ///   <c>true</c> if the specified date is the first day of the week;   
8:  ///   otherwise, <c>false</c>.  
9:  /// </returns>  
10:  public static bool IsFirstDayOfWeek(this DateTime value)  
11:  {  
12:    // Gets the first day of the week for the current culture info.  
13:    DayOfWeek firstDayOfWeek = DateTimeUtils.GetFirstDayOfWeek();  
14:    
15:    bool isFirstDayOfWeek = firstDayOfWeek == value.DayOfWeek;  
16:    
17:    return isFirstDayOfWeek;  
18:  }  

Finally I needed an extension method that could check if a specific date is the last day of the week. There is no property on CultureInfo.CurrentCulture that could tell me that, so I had to find another solution, and the solution turned out to be very simple. If I have a specific date, and the next day is the first day of the week, then it is the last day of the week.

Next I just had to create a extension method to check just that:
1:    
2:  /// <summary>  
3:  /// Gets a value indicating whether the specified date is the first day of the week.  
4:  /// </summary>  
5:  /// <param name="value">The value.</param>  
6:  /// <returns>  
7:  ///   <c>true</c> if the specified date is the first day of the week;   
8:  ///   otherwise, <c>false</c>.  
9:  /// </returns>  
10:  public static bool IsLastDayOfWeek(this DateTime value)  
11:  {  
12:    // Gets the first day of the week for the current culture info.  
13:    DayOfWeek firstDayOfWeek = DateTimeUtils.GetFirstDayOfWeek();  
14:    
15:    // Gets the day of week for the next day  
16:    DayOfWeek nextDayOfWeek = value.AddDays(1).DayOfWeek;  
17:    
18:    // Checks if the next day is the first day of the week, because then the  
19:    // day before is the last day of the week.  
20:    var isFirstDayOfWeek = firstDayOfWeek == nextDayOfWeek;  
21:    
22:    return isFirstDayOfWeek;  
23:  }  

No comments:

Post a Comment