C# 星期几的整数表示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/757665/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:03:26  来源:igfitidea点击:

Integer representation for day of the week

c#datetimeparsing

提问by THE DOCTOR

I would like to convert a date object its integer representation for the day of week in C#. Right now, I am parsing a XML file in order to retrieve the date and storing that info in a string. It is in the following format:

我想在 C# 中将日期对象转换为星期几的整数表示。现在,我正在解析一个 XML 文件以检索日期并将该信息存储在一个字符串中。它采用以下格式:

"2008-12-31T00:00:00.0000000+01:00"

“2008-12-31T00:00:00.0000000+01:00”

How can I take this and convert it into a number between 1 and 7 for the day of the week that it represents?

我怎样才能把它转换成它所代表的星期几的 1 到 7 之间的数字?

采纳答案by Daniel Brückner

(Int32)Convert.ToDateTime("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1

回答by Jason Coyne

If you load that into a DateTime varible, DateTime exposes an enum for the day of the week that you could cast to int.

如果您将其加载到 DateTime 变量中,则 DateTime 会公开您可以转换为 int 的星期几的枚举。

回答by Erv Walter

DateTime date = DateTime.Parse("2008-12-31T00:00:00.0000000+01:00");
int dayOfWeek = (int)date.DayOfWeek + 1; //DayOfWeek is 0 based, you wanted 1 based

回答by pmarflee

(int)System.DateTime.Parse("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1

(int)System.DateTime.Parse("2008-12-31T00:00:00.0000000+01:00").DayOfWeek + 1