C# 返回索引值 datetime.now.dayofweek 但如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/595000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return index value datetime.now.dayofweek but how?
提问by Penguen
is there any function of datetime return dayofweekindex? such as:
int Todaywhat_is_Index= = DateTime.Now.IndexOfDayofThisWeek;
if Today is friday, it must be return 5
ifToday is Saturday, it must be return 6
ifToday is Sunday, it must be return 0
是否有日期时间返回 dayofweekindex 的任何功能?如:
int Todaywhat_is_Index== DateTime.Now.IndexOfDayofThisWeek;
如果今天是星期五,则必须返回 5 如果今天是
星期六,则必须返回 6如果今天是
星期日,则必须返回 0
采纳答案by Gerrie Schenck
Yes, check DateTime.DayOfWeek.
是的,检查DateTime.DayOfWeek。
Please note this depends on the regional settings (here in Europe Monday is the first day of the week).
请注意,这取决于区域设置(在欧洲,星期一是一周的第一天)。
回答by Henk Holterman
This little one-liner works independent of locale, with always Friday == 5
这个小小的单线工作独立于语言环境,总是星期五 == 5
int x = (int)System.Globalization.CultureInfo
.InvariantCulture.Calendar.GetDayOfWeek(DateTime.Now);
回答by Ziv Bar-Lev
Try this:
尝试这个:
int dayOfWeek;
dayOfWeek = (int)System.DateTime.Now.DayOfWeek - (int)System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + 1