字符串的日期时间格式?

5 .net datetime

如何将字符串转换为DateTime格式?例如,如果我有一个字符串,如:

"24/10/2008"

如何将其转换为DateTime格式?

Jon*_*eet 12

使用DateTime.ParseExact:

string str = "24/10/2008";
DateTime dt = DateTime.ParseExact(str, "dd/MM/yyyy", 
                                  Thread.CurrentThread.CurrentCulture);
Run Code Online (Sandbox Code Playgroud)

(诚​​然,你应该考虑你真正想要解析它的文化.)

编辑:其他答案指定"null"作为第三个参数 - 这相当于使用Thread.CurrentThread.CurrentCulture.

有关其他格式,请参阅MSDN中的"自定义日期和时间格式字符串".