Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change which day is the first day of the week?

I need to change the first day of the week in asp.net, i.e. I want Saturday to be the first day of the week.

For example, the code below should return 3 on Monday:

(int)DateTime.Now.DayOfWeek
like image 544
Ghooti Farangi Avatar asked Oct 09 '11 12:10

Ghooti Farangi


2 Answers

You cannot influence the value of DateTime.DayOfWeek as this is a type of System.DayOfWeek which is an enumeration (i.e. the values are constant). The definition for System.DayOfWeek is in the code block below. So, if you want to treat DayOfWeek as though it were 3 on Monday and Saturday to be the first day of the week, then I have to assume you want a 1 to 7 based numbering system. In that case, you can do ((int)DateTime.Now.DayOfWeek+1) % 7 + 1 to get your desired numbers. If you don't need this for calculations, it would be better to just compare the value of DateTime.Now.DayOfWeek to its enum constants (e.g. if( DateTime.Now.DayOfWeek == DayOfWeek.Monday ) ...).

// Summary:
//     Specifies the day of the week.
[Serializable]
[ComVisible(true)]
public enum DayOfWeek
{
    // Summary:
    //     Indicates Sunday.
    Sunday = 0,
    //
    // Summary:
    //     Indicates Monday.
    Monday = 1,
    //
    // Summary:
    //     Indicates Tuesday.
    Tuesday = 2,
    //
    // Summary:
    //     Indicates Wednesday.
    Wednesday = 3,
    //
    // Summary:
    //     Indicates Thursday.
    Thursday = 4,
    //
    // Summary:
    //     Indicates Friday.
    Friday = 5,
    //
    // Summary:
    //     Indicates Saturday.
    Saturday = 6,
}
like image 59
Drew Chapin Avatar answered Nov 02 '22 23:11

Drew Chapin


Use the following code to change the first day of week in an asp.net application.

CultureInfo _culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
CultureInfo _uiculture = (CultureInfo)CultureInfo.CurrentUICulture.Clone();

_culture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;
_uiculture.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;

System.Threading.Thread.CurrentThread.CurrentCulture = _culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = _uiculture;
like image 42
Babuji Godem Avatar answered Nov 03 '22 00:11

Babuji Godem