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,
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,
You could use:
DateTime.Parse(datestring);
or
DateTime.TryParse(string, IFormatProvider, DateTimeStyles, out DateTime)
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.
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.
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