C# DateTime Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To get the start DateTime
of a week in C#, you can use the DayOfWeek
property of a DateTime
object to determine the current day of the week, and then subtract the appropriate number of days to get to the start of the week. Here's an example:
DateTime inputDate = DateTime.Now; DateTime startOfWeek = inputDate.AddDays(-(int)inputDate.DayOfWeek);
In the above example, we have a DateTime
object that represents the input date. We use the DayOfWeek
property to get the current day of the week, and then subtract the appropriate number of days to get to the start of the week. In C#, the DayOfWeek
enumeration assigns Sunday a value of 0 and Saturday a value of 6, so subtracting the DayOfWeek
value from the input date gives us the start of the week.
To get the current week number of a given date in C#, you can use the Calendar.GetWeekOfYear
method to get the week number of a DateTime
object. Here's an example:
DateTime inputDate = DateTime.Now; CultureInfo culture = CultureInfo.CurrentCulture; Calendar calendar = culture.Calendar; int weekNumber = calendar.GetWeekOfYear(inputDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
In the above example, we have a DateTime
object that represents the input date. We create a CultureInfo
object to represent the current culture, and a Calendar
object to represent the calendar used by the culture. We use the Calendar.GetWeekOfYear
method to get the week number of the input date, using the CalendarWeekRule.FirstFourDayWeek
enumeration to specify the first week of the year, and the DayOfWeek.Monday
enumeration to specify the start of the week.
To get the next Friday of a given date in C#, you can use a loop to iterate over each day after the input date, and then check if each day is a Friday using the DayOfWeek
property of the DateTime
object. Here's an example:
DateTime inputDate = DateTime.Now; DateTime nextFriday = inputDate; while (nextFriday.DayOfWeek != DayOfWeek.Friday) { nextFriday = nextFriday.AddDays(1); }
In the above example, we have a DateTime
object that represents the input date. We use a while
loop to iterate over each day after the input date, setting nextFriday
to the next day each time through the loop. We use the DayOfWeek
property of the DateTime
object to check if each day is a Friday. When we find a Friday, the loop exits, and we get the resulting DateTime
object which represents the next Friday.
C# calculate week number from DateTime: This involves determining the week number within a year based on a given DateTime.
DateTime myDate = DateTime.Now; int weekNumber = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(myDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); Console.WriteLine($"Week number for {myDate}: {weekNumber}");
Week of the year calculation using DateTime in C#: This is similar to the first point but may use different rules for week calculation.
DateTime myDate = DateTime.Now; int weekNumber = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(myDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); Console.WriteLine($"Week number for {myDate}: {weekNumber}");
ISO week date system in C#: ISO week numbering system defines weeks starting from Monday and considers the week with the year's first Thursday as the first week.
DateTime myDate = DateTime.Now; int isoWeekNumber = ISOWeek.GetWeekOfYear(myDate); Console.WriteLine($"ISO Week number for {myDate}: {isoWeekNumber}");
Get week of month from DateTime in C#: To get the week number within a month.
DateTime myDate = DateTime.Now; int weekNumber = (myDate.Day - 1) / 7 + 1; Console.WriteLine($"Week number of month for {myDate}: {weekNumber}");
Week starting on Monday vs. Sunday in C#: This involves adjusting the calculation based on whether the week starts on Monday or Sunday.
DateTime myDate = DateTime.Now; DayOfWeek firstDayOfWeek = DayOfWeek.Monday; // or DayOfWeek.Sunday int weekNumber = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(myDate, CalendarWeekRule.FirstDay, firstDayOfWeek); Console.WriteLine($"Week number for {myDate}: {weekNumber}");
Calculating week range for a given DateTime in C#: To find the start and end dates of the week containing a given DateTime.
DateTime myDate = DateTime.Now; DateTime startOfWeek = myDate.AddDays(-(int)myDate.DayOfWeek); DateTime endOfWeek = startOfWeek.AddDays(6); Console.WriteLine($"Week range for {myDate}: {startOfWeek} - {endOfWeek}");
C# CultureInfo and week calculation: Using a specific CultureInfo for week calculation.
DateTime myDate = DateTime.Now; CultureInfo customCulture = new CultureInfo("en-US"); // or any other culture int weekNumber = customCulture.Calendar.GetWeekOfYear(myDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); Console.WriteLine($"Week number for {myDate} using custom culture: {weekNumber}");
Handling week transitions in C# DateTime: Managing cases where a week might transition between two years.
DateTime myDate = DateTime.Now; int year = myDate.Year; int weekNumber = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(myDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); if (weekNumber == 1 && myDate.Month == 12) { year--; // Adjusting for week transitioning to the next year }
ISO 8601 week date format in C#: Formatting a DateTime to the ISO 8601 week date format.
DateTime myDate = DateTime.Now; string isoWeekDate = myDate.ToString("yyyy-'W'ww"); Console.WriteLine($"ISO 8601 Week Date for {myDate}: {isoWeekDate}");
Calculating workweek and weekend in C#: Determining whether a given DateTime falls on a workweek or weekend.
DateTime myDate = DateTime.Now; if (myDate.DayOfWeek != DayOfWeek.Saturday && myDate.DayOfWeek != DayOfWeek.Sunday) { Console.WriteLine($"{myDate} is a workday."); } else { Console.WriteLine($"{myDate} is a weekend day."); }