C#如何将String转换为时间和日期格式?

Jav*_*oob 3 c# datetime

我有一个简短的程序,它将字符串转换为简单字符串的日期和时间格式.

但是,由于字符串的顺序,系统似乎无法将字符串转换为日期时间格式.应该转换的字符串就像一个例子:"Thu Dec 9 05:12:42 2010"

该方法Convert.ToDateTime已被使用但不起作用.

有人可以告知代码吗?谢谢!

String re = "Thu Dec  9 05:12:42 2010";

DateTime time = Convert.ToDateTime(re);

Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)

Geo*_*ord 6

看看DateTime.TryParseExact

DateTime time; 
if (DateTime.TryParseExact(re,
     "ddd MMM d hh:mm:ss yyyy", CultureInfo.CurrentCulture, 
      DateTimeStyles.None, out time)) {

    Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
} else {
    Console.WriteLine("'{0}' is not in an acceptable format.", re);
}
Run Code Online (Sandbox Code Playgroud)