在C#中将日期和时间字符串转换为DateTime

Tro*_*orf 4 c# datetime converter

我有以下列格式显示日期和时间的字符串:

2013年1月3日星期四15:04:29

我如何将其转换为DateTime?我试过了:

string strDateStarted = "Thu Jan 03 15:04:29 2013"
DateTime datDateStarted = Convert.ToDateTime(strDateStarted);
Run Code Online (Sandbox Code Playgroud)

但这不起作用.在我的程序中,此值从日志文件中读取,因此我无法更改文本字符串的格式.

Ode*_*ded 9

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

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

我建议阅读自定义日期和时间格式字符串.

在这种情况下,相应的格式字符串将是:

"ddd MMM dd HH:mm:ss yyyy"
Run Code Online (Sandbox Code Playgroud)

要使用的:

DateTime.ParseExact("Thu Jan 03 15:04:29 2013", 
                    "ddd MMM dd HH:mm:ss yyyy", 
                    CultureInfo.InvariantCulture)
Run Code Online (Sandbox Code Playgroud)