Jonas Stawski
Jonas Stawski

Reputation: 6752

How to convert string with unusual format into 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.

Upvotes: 4

Views: 5520

Answers (9)

Adam Porad
Adam Porad

Reputation: 14470

You can use DateTime.ParseExact or DateTimeOffset.ParseExact to specify the format of the date string.

Although, I wasn't able to quickly figure out how to match the timezone specifier (i.e. GMT). A look at a few Google results, shows that most people who are trying to solve this are doing it heuristically -- making a list of all time zones and the offsets and then parsing the string and replacing the timezone specifier with the +/- offset, or some other sort of hackish approach. Although, none of those solutions were from StackOverflow, so who knows how good they are.

Here's a short example I wrote, with the "GMT" stripped from the date string trying to be converted. If you could replace the timezone with the offset, add "zzz" to the format string. For the parse other formats, heres the MSDN page Custom Date and Time Format Strings that lists them all.

// Parse date and time with custom specifier.
string dateString = "Tue Jan 20 20:47:43 2009";
string format = "ddd MMM dd HH:mm:ss yyyy";
DateTime result;
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;

try
{
   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);
}

Upvotes: 0

Armstrongest
Armstrongest

Reputation: 15430

DateTime dt;
if(DateTime.TryParse("Tue Jan 20 20:47:43 GMT 2009", out dt)){
   /* Yay.. it's valid */
}

You can also use TryParseExact where you can specify the format of your DateTime

Using TryparseExact

const string FORMAT = "ddd MMM dd HH:mm:ss \"GMT\" yyyy";
if (DateTime.TryParseExact("Tue Jan 20 20:47:43 GMT 2009", FORMAT, CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out dt)) {
        /* is valid */
 }    

I believe that should work. Not sure if it will try to parse out the GMT though.

Upvotes: 2

AndiDog
AndiDog

Reputation: 70249

There you go

DateTime d = DateTime.ParseExact("Tue Jan 20 20:47:43 GMT 2009".Replace("GMT", "+00"), "ddd 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).

Upvotes: 2

ΩmegaMan
ΩmegaMan

Reputation: 31721

CultureInfo enUS = new CultureInfo( "en-US" ); 

DateTime dt = DateTime.ParseExact( "Tue Jan 20 19:47:43 GMT 2009", "ddd MMM dd HH:mm:ss 'GMT' yyyy", enUS, DateTimeStyles.None );

Console.WriteLine( dt.ToString() );

Upvotes: 0

TLiebe
TLiebe

Reputation: 7996

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

Upvotes: 11

Raja
Raja

Reputation: 3618

Try this:

DateTime.TryParse(Tue Jan 20 20:47:43 GMT 2009", out objDt);

You need to give an output value. Use If and if it returns true then its a valid date.

HTH

Upvotes: 0

Dan H
Dan H

Reputation: 1828

Try to do a DateTime.Parse("Tue Jan 20 20:47:43 GMT 2009") and see if it accepts it.

Here's a good link for custom DateTime formatting.

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

I hope that helps.

Upvotes: 0

Marek Karbarz
Marek Karbarz

Reputation: 29324

You could use Convert.ToDateTime

Upvotes: 0

Dustin Laine
Dustin Laine

Reputation: 38553

DateTime.Parse("Tue Jan 20 20:47:43 GMT 2009")

Upvotes: -1

Related Questions