在 C# 中将日期和时间字符串转换为 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15570812/
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
Convert date and time string to DateTime in C#
提问by TroggleDorf
I have string that displays date and time in the following format:
我有按以下格式显示日期和时间的字符串:
Thu Jan 03 15:04:29 2013
2013 年 1 月 3 日星期四 15:04:29
How would I convert this to a DateTime? I have tried:
我将如何将其转换为 DateTime?我试过了:
string strDateStarted = "Thu Jan 03 15:04:29 2013"
DateTime datDateStarted = Convert.ToDateTime(strDateStarted);
But this does not work. In my program this value is read in from a log file so I am not able to change the format of the text string.
但这不起作用。在我的程序中,该值是从日志文件中读入的,因此我无法更改文本字符串的格式。
采纳答案by Arshad
Use following code :
使用以下代码:
string strDateStarted = "Thu Jan 03 15:04:29 2013";
DateTime datDateStarted;
DateTime.TryParseExact(strDateStarted, new string[] { "ddd MMM dd HH:mm:ss yyyy" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out datDateStarted);
Console.WriteLine(datDateStarted);
and sure if time is in 24 HRS format , then use HH. More details
并确定时间是否为 24 HRS 格式,然后使用 HH。更多细节
回答by SWeko
Try using DateTime.ParseExact
.
尝试使用DateTime.ParseExact
.
in your case the format specifiedshould be:
Thu Jan 03 15:04:29 2013
and the call should look like:
调用应该如下所示:
DateTime logDate = DateTime.ParseExact(logValue, "ddd MMM dd HH:mm:ss yyyy",
CultureInfo.CreateSpecificCulture("en-US"));
The third parameter is set to the US culture, so that the ddd
and MMM
parts, correspond to Thu
and Jan
respectively.
第三个参数设置为美国文化,使ddd
和MMM
部分分别对应Thu
和Jan
。
In this case I'm recommending ParseExact
instead of TryParseExact
, because of the source of the data. If you are parsing user input, always use TryParseExact
, because you cannot trust that the user followed the requested format. However, in this case, the source is a file with a well defined format, so any invalid data should be treated as exception, because they are, well, exceptional.
在这种情况下,我推荐ParseExact
而不是TryParseExact
,因为数据的来源。如果您正在解析用户输入,请始终使用TryParseExact
,因为您不能相信用户遵循了请求的格式。但是,在这种情况下,源是一个格式定义明确的文件,因此任何无效数据都应视为异常,因为它们是异常的。
Also note that *ParseExact
methods are very unforgiving. If the data does not exactly conform to the format specified, it's treated as an error.
另请注意,*ParseExact
方法是非常无情的。如果数据不完全符合指定的格式,则将其视为错误。
回答by vidario
Have a look at DateTime.TryParse()
回答by Oded
Use one of the *Parse*
methods defined on DateTime
.
使用 上*Parse*
定义的方法之一DateTime
。
Either TryParseExact
or ParseExact
which will take a format string corresponding to the date string.
无论是TryParseExact
或ParseExact
将采取相应的日期字符串格式字符串。
I suggest reading up on Custom Date and Time Format Strings.
我建议阅读自定义日期和时间格式字符串。
In this case, the corresponding format string would be:
在这种情况下,相应的格式字符串将是:
"ddd MMM dd HH:mm:ss yyyy"
To be used:
要使用的:
DateTime.ParseExact("Thu Jan 03 15:04:29 2013",
"ddd MMM dd HH:mm:ss yyyy",
CultureInfo.InvariantCulture)
回答by csharp newbie
string yourDateTimeRepresentation = "R"; //for example
DateTime dt = DateTime.ParseExact(strDateStarted , yourDateTimeRepresentation , System.Globalization.CultureInfo.CurrentCulture);