How to get the integer value of day of week

匆匆过客 提交于 2019-11-26 15:51:49

问题


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

回答1:


Use

day1 = (int)ClockInfoFromSystem.DayOfWeek;



回答2:


int day = (int)DateTime.Now.DayOfWeek;

First day of the week: Sunday (with a value of zero)




回答3:


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;



回答4:


day1= (int)ClockInfoFromSystem.DayOfWeek;



回答5:


Try this. It will work just fine:

int week = Convert.ToInt32(currentDateTime.DayOfWeek);



回答6:


The correct way to get the integer value of an Enum such as DayOfWeek as a string is:

DayOfWeek.ToString("d")



回答7:


Another way to get Monday with integer value 1 and Sunday with integer value 7

int day = ((int)DateTime.Now.DayOfWeek + 6) % 7 + 1;



回答8:


DateTime currentDateTime = DateTime.Now;
int week = (int) currentDateTime.DayOfWeek;


来源:https://stackoverflow.com/questions/9199080/how-to-get-the-integer-value-of-day-of-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!