tags:

views:

492

answers:

4

I'd like to convert a bunch of date strings like the following Mon Aug 7 15:32:52 GMT+0900 2007 to C# datetime objects.

Is there anything built in to the .net framework to do this or will I have to parse the string into date parts?

Many thanks,

+2  A: 

You could use:

 DateTime.Parse(datestring);

or

DateTime.TryParse(string, IFormatProvider, DateTimeStyles, out DateTime)
Robban
Thanks - Appreciated :)
Chin
+1  A: 

Look at the DateTime.Parse method. You can use the DateTimeFormatInfo class as IFormatProvider. There you could specify the format of the date you want to parse.

Ikke
A: 

That looks like a simple RFC formatted date, so a straight DateTime.Parse as Ikke said will work and you shouldn't have to provide the format. You can pass a DateTime object as the second argument in the DateTime.TryParse method to see whether it fails or not, as it returns a boolean.

Chris S
+1  A: 

Im not sure what "date strings like the following" means since seems you forgot to provide a example. But maybe if you try this.

string date = DateTime.Today.ToString("ddd MMM d HH:mm:ss G'M'Tzzz yyyy", CultureInfo.CreateSpecificCulture("en-EN"));
date = date.Remove(date.LastIndexOf(':'), 1);
// Do whatever you want with the date string
// Output looks like Wed Sep 9 00:00:00 GMT+0200 2009
kripto_ash
`Mon Aug 7 15:32:52 GMT+0900 2007` is in there :)
Chris S
he placed a "to" before the actual date string which made me think he wanted to convert "to" that format. :\
kripto_ash
I've cleaned it up. Sorry for the confusion and thanks for the help.
Chin