How to convert string with unusual format into datetime

前端 未结 9 934
半阙折子戏
半阙折子戏 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:53

    You can use the DateTime.TryParseExact() method with a suitable format string. See here

    EDIT: Try something like this:

            DateTime dt;
            System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US"); 
    
            if ( DateTime.TryParseExact( "Tue Jan 20 20:47:43 GMT 2009", "ffffd MMM dd H:mm:ss \"GMT\" yyyy", enUS, System.Globalization.DateTimeStyles.NoCurrentDateDefault , out dt  ))
            {
                Console.WriteLine(dt.ToString() );
            }
    

提交回复
热议问题