我收到以下格式的Flash日期时间: Sun Jul 17 20:43:02 GMT-0600 2011
默认的DateTime.Parse方法无法将其识别为有效的日期时间。有没有办法解析它以获得有效的UTC DateTime?
答案 0 :(得分:1)
您应该查看采用格式的DateTime
类的解析方法,它们在阅读遵循不同格式的日期时更灵活。看看这些内容是否有效(可随意更改格式):
// Parse date and time with custom specifier.
string dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
string format = "ddd dd MMM yyyy h:mm tt zzz";
CultureInfo provider = CultureInfo.InvariantCulture;
try {
DateTime result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
此处有更多示例:
答案 1 :(得分:0)
此示例的确切格式如下
var d = "Sun Jul 17 20:43:02 GMT-0600 2011";
var dt = DateTime.ParseExact(d, "ddd MMM dd HH:mm:ss 'GMT'zz'00' yyyy", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString());