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 
+6
source share
4 answers

You cannot influence the value of DateTime.DayOfWeek , since it is a type of System.DayOfWeek , which is an enumeration (i.e. the values ​​are constant). The definition for System.DayOfWeek given in the code block below. So, if you want to treat DayOfWeek as 3 on Monday and Saturday to be the first day of the week, then I have to assume that you need a 1-7 numbering system. In this case, you can do ((int)DateTime.Now.DayOfWeek+1) % 7 + 1 to get the numbers you need. If you do not need this for calculations, it would be better to simply compare the value of DateTime.Now.DayOfWeek with its enumeration constants (for example, 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, } 
+5
source

Use the following code to change the first day of the 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; 
+4
source

I suppose you just need to change the culture you use ( DayOfWeek Enumeration ).

 Thread.CurrentThread.CurrentCulture = New CultureInfo("ar-EG") 
+1
source

I created this C # extension method that returns a zero based index based on an alternate first day of the week.

DayOfWeek default DayOfWeek is Sunday = 0, Monday = 1, ..., Saturday = 6. We want (for example) DayOfWeek be on Monday = 0, Tuesday = 1, ..., Sunday = 6.

Given that the current weekday is Sunday:

 DateTime.Now.DayOfWeek; // returns Sunday (int)DateTime.Now.DayOfWeek; // returns 0 DateTime.Now.DayOfWeek(DayOfWeek.Monday); // returns 6 public static class ExtensionMethods { /// <summary> /// Returns an zero-based index where firstDayOfWeek = 0 and lastDayOfWeek = 6 /// </summary> /// <param name="value"></param> /// <param name="firstDayOfWeek"></param> /// <returns>int between 0 and 6</returns> public static int DayOfWeek(this DateTime value, DayOfWeek firstDayOfWeek) { var idx = 7 + (int)value.DayOfWeek - (int)firstDayOfWeek; if (idx > 6) // week ends at 6, because Enum.DayOfWeek is zero-based { idx -= 7; } return idx; } } 
0
source

Source: https://habr.com/ru/post/898920/


All Articles