2 February 2023
C#

Getting the first day in a week with C#

For my Blazor Calendar component, I ran into a small problem to determine the first day of the week. It’s not new and the web is full of solutions that in the end… well, they are only based on Saturday or Sunday. Except that there are countries like Afghanistan, Iran and Somalia where the first day of the week is Saturday.

Saturday? Sunday ? Monday ? Friday!

In fact it’s not that simple because it should be noted that it can vary according to the culture and traditions of each region. Unless I go to each country to investigate, the web tells me everything and its opposite. If I look at Wikipedia, I find this map:

First Day of Week World Map

And obviously in Maldives it is a Friday

source : https://commons.wikimedia.org/wiki/File_talk:First_Day_of_Week_World_Map.svg

ChatGPT, Stack Overflow, help!

Well, there are no good answers for me. And the following code using simply DateTime.FirstDayOfWeek, finally returns me Sunday.

<code>CultureInfo info = new CultureInfo("dz-DZ");
Console.WriteLine(info.DateTimeFormat.FirstDayOfWeek);</code>

My need is to give him the day of the month and he returns the number of the first day of the week. Previously I will have specified in my code the Culture.

I then start on the day numbers like DayOfWeek with 0 for Sunday, 1 for Monday and so on.

<code>/// &lt;summary>
	/// Returns the first day of the week for the day of a month.
	/// The order of the days of the week is different in each country
	/// DayOfWeek : 
	///   .Sunday    = 0
	///   .Monday    = 1
	///   .Tuesday   = 2
	///   .Wednesday = 3
	///   .Thursday  = 4
	///   .Friday    = 5
	///   .Saturday  = 6
	///
	/// => if Sunday is the First Day   =>   0  1  2  3  4  5  6
	/// => if Monday is the First Day   =>   1  2  3  4  5  6  0
	/// => if Saturday is the First Day =>   6  0  1  2  3  4  5
    ///
	/// To test : 
	/// CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("dz-DZ"); // firstDayOfWeek = Saturday
	/// CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fr-FR"); // firstDayOfWeek = Monday
	/// CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); // firstDayOfWeek = Sunday
	/// &lt;/summary>
	/// &lt;param name="numOfDay">&lt;/param>
	/// &lt;returns>&lt;/returns>
	public static int GetNumOfDay(int numOfDay)
    {
        if (numOfDay &lt; 0) return 0;
        if (numOfDay > 6)
        {
            // Whatever the value I transform it in the place from 0 to 6
            numOfDay %= 7;
        }

        var firstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

        if (firstDayOfWeek == DayOfWeek.Saturday)
        {
            if (numOfDay == 0) numOfDay = 6;
            if (numOfDay == 7) numOfDay = 0;
        }
        else if (firstDayOfWeek == DayOfWeek.Monday)
        {
            if (numOfDay >= 0) numOfDay++;
            if (numOfDay == 7) numOfDay = 0;
        }

        return numOfDay;
    }</code>

And now everything is fine!

All is well? Well ok, I didn’t manage the Maldives. If I have a maldivian as a user, I have an improvement to make

Leave a Reply