在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:13:33  来源:igfitidea点击:

Convert date and time string to DateTime in C#

c#datetimeconverter

提问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 dddand MMMparts, correspond to Thuand Janrespectively.

第三个参数设置为美国文化,使dddMMM部分分别对应ThuJan



In this case I'm recommending ParseExactinstead 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 *ParseExactmethods are very unforgiving. If the data does not exactly conform to the format specified, it's treated as an error.

另请注意,*ParseExact方法是非常无情的。如果数据不完全符合指定的格式,则将其视为错误。

回答by vidario

回答by Oded

Use one of the *Parse*methods defined on DateTime.

使用 上*Parse*定义的方法之一DateTime

Either TryParseExactor ParseExactwhich will take a format string corresponding to the date string.

无论是TryParseExactParseExact将采取相应的日期字符串格式字符串。

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);