如何用.NET解析Flash Date字符串?

时间:2011-07-18 01:33:52

标签: .net datetime formatting

我收到以下格式的Flash日期时间: Sun Jul 17 20:43:02 GMT-0600 2011

默认的DateTime.Parse方法无法将其识别为有效的日期时间。有没有办法解析它以获得有效的UTC DateTime?

2 个答案:

答案 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);
}

此处有更多示例:

http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

答案 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());