How do I get the day of a week in integer format? I know ToString will return only a string.
DateTime ClockInfoFromSystem = DateTime.Now;
int day1;
string day2;
day1= ClockInfoFromSystem.DayOfWeek.ToString(); /// it is not working
day2= ClockInfoFromSystem.DayOfWeek.ToString(); /// it gives me string
Use
day1 = (int)ClockInfoFromSystem.DayOfWeek;
int day = (int)DateTime.Now.DayOfWeek;
First day of the week: Sunday (with a value of zero)
If you want to set first day of the week to Monday with integer value 1 and Sunday with integer value 7
int day = ((int)DateTime.Now.DayOfWeek == 0) ? 7 : (int)DateTime.Now.DayOfWeek;
day1= (int)ClockInfoFromSystem.DayOfWeek;
RaviKant Hudda
Try this. It will work just fine:
int week = Convert.ToInt32(currentDateTime.DayOfWeek);
The correct way to get the integer value of an Enum such as DayOfWeek as a string is:
DayOfWeek.ToString("d")
DateTime currentDateTime = DateTime.Now;
int week = (int) currentDateTime.DayOfWeek;
Another way to get Monday with integer value 1 and Sunday with integer value 7
int day = ((int)DateTime.Now.DayOfWeek + 6) % 7 + 1;
来源:https://stackoverflow.com/questions/9199080/how-to-get-the-integer-value-of-day-of-week