开发者

How to parse a Flash Date string with .NET?

开发者 https://www.devze.com 2023-03-20 18:56 出处:网络
I am receiving a Flash datetime in the following format: Sun Jul 17 20:43:02 GMT-0600 2011 The default开发者_C百科 DateTime.Parse method does not recognize it as a valid datetime.Is there a way to pa

I am receiving a Flash datetime in the following format: Sun Jul 17 20:43:02 GMT-0600 2011

The default开发者_C百科 DateTime.Parse method does not recognize it as a valid datetime. Is there a way to parse it to get a valid UTC DateTime?


You should look at the parse methods on the DateTime class that take a format, they are more flexible in reading dates that follow different formats. See if something along these lines works (feel free to change the format):

// 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);
}

More examples here:

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


Your exact format for this example would be the following

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消