Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string with unusual format into datetime

Tags:

c#

datetime

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 question, what is the name of that format? Second question, what's the easiest and clearest way to convert this string into a datetime? I would love to be able to use a .net API/Helper method if possible.

Edit: I forgot to mention that I've already tried using DateTime.Parse and Convert.ToDateTime. None of those worked.

like image 321
Jonas Stawski Avatar asked Dec 09 '22 16:12

Jonas Stawski


1 Answers

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", "ddd MMM dd H:mm:ss \"GMT\" yyyy", enUS, System.Globalization.DateTimeStyles.NoCurrentDateDefault , out dt  ))
        {
            Console.WriteLine(dt.ToString() );
        }
like image 63
TLiebe Avatar answered Dec 12 '22 07:12

TLiebe