How to convert string with unusual format into datetime

前端 未结 9 937
半阙折子戏
半阙折子戏 2021-01-19 17:15

I\'m using .NET 3.5 and I have a date that comes in as string in the following format:

Tue Jan 20 20:47:43 GMT 2009

First questi

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 17:45

    There you go

    DateTime d = DateTime.ParseExact("Tue Jan 20 20:47:43 GMT 2009".Replace("GMT", "+00"), "ffffd MMM dd H:mm:ss zz yyyy", new CultureInfo("en-US"));
    

    The DateTime API and its documentation pretty much sucks. Exceptions will only tell you that "String was not recognized as a valid DateTime", which doesn't really help. It had to figure out the date format specifiers myself because I didn't find them in MSDN.

    The "en-US" locale is necessary, I guess, because your date format uses English abbreviations like "Tue".

    Anyway, I can't tell you what the date format is called. It is pretty similar but not equal to a format used with HTTP (e.g. If-Modified-Since: Wed, 08 Dec 2004 13:25:25 GMT).

提交回复
热议问题