C# || How To Get First/Last Day Of The Week And The First/Last Day Of The Month
The following is a module with functions which demonstrates how to get the first and last day of the week, as well as how to get the first and last day of the month using C#.
1. First & Last Day Of Week
The example below demonstrates the use of ‘Utils.DateRange.GetWeekStartAndEnd‘ to get the first and last day of the week.
By default, the start of the week is set on Monday. This can be changed by setting the second parameter to any valid property contained in the System.DayOfWeek enum.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// First & Last Day Of Week // Get the first and last day of the week var weekResult = Utils.DateRange.GetWeekStartAndEnd(DateTime.Today); // Display results Console.WriteLine($@" Today = {DateTime.Today.ToShortDateString()} Week Start = {weekResult.StartDate.ToShortDateString()} Week End = {weekResult.EndDate.ToShortDateString()} "); // expected output: /* Today = 5/7/2021 Week Start = 5/3/2021 Week End = 5/9/2021 */ |
2. First & Last Day Of Month
The example below demonstrates the use of ‘Utils.DateRange.GetMonthStartAndEnd‘ to get the first and last day of the week.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// First & Last Day Of Month // Get the first and last day of the month var monthResult = Utils.DateRange.GetMonthStartAndEnd(DateTime.Today); // Display results Console.WriteLine($@" Today = {DateTime.Today.ToShortDateString()} Month Start = {monthResult.StartDate.ToShortDateString()} Month End = {monthResult.EndDate.ToShortDateString()} "); // expected output: /* Today = 5/7/2021 Month Start = 5/1/2021 Month End = 5/31/2021 */ |
3. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 7, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public class DateRange { public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } /// <summary> /// Takes in a date and returns the first and last day of the week /// </summary> /// <param name="date">The reference date in question</param> /// <param name="firstDayOfWeek">The day the week starts on</param> /// <returns>An object that contains the date range result</returns> public static DateRange GetWeekStartAndEnd(DateTime date, DayOfWeek firstDayOfWeek = DayOfWeek.Monday) { DateRange result = new DateRange(); result.StartDate = date.AddDays(1 - Weekday(date, firstDayOfWeek)).Date; result.EndDate = result.StartDate.AddDays(6).Date; return result; } /// <summary> /// Takes in a date and returns the first and last day of the month /// </summary> /// <param name="date">The reference date in question</param> /// <returns>An object that contains the date range result</returns> public static DateRange GetMonthStartAndEnd(DateTime date) { DateRange result = new DateRange(); result.StartDate = new DateTime(date.Year, date.Month, 1).Date; result.EndDate = result.StartDate.AddMonths(1).AddDays(-1).Date; return result; } private static int Weekday(DateTime date, DayOfWeek firstDayOfWeek) { return ((date.DayOfWeek - firstDayOfWeek + 7) % 7) + 1; } } }// http://programmingnotes.org/ |
4. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 7, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; public class Program { static void Main(string[] args) { try { var weekResult = Utils.DateRange.GetWeekStartAndEnd(DateTime.Today); Display($@" Today = {DateTime.Today.ToShortDateString()} Week Start = {weekResult.StartDate.ToShortDateString()} Week End = {weekResult.EndDate.ToShortDateString()} "); var monthResult = Utils.DateRange.GetMonthStartAndEnd(DateTime.Today); Display($@" Today = {DateTime.Today.ToShortDateString()} Month Start = {monthResult.StartDate.ToShortDateString()} Month End = {monthResult.EndDate.ToShortDateString()} "); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply