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
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,
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With