views:

130

answers:

7

Well,

This is a string I get from a web service:

"Tuesday, March 30, 2010 10:45 AM"

and I need to convert it to a DateTime.

Do you know a simple way to achieve this?

Thanks,

+7  A: 
    string strDateTime = "Tuesday, March 30, 2010 10:45 AM"; 

    DateTime myDateTime = DateTime.Parse(strDateTime);
Jason W
+2  A: 

DateTime.Parse or DateTime.ParseExact should do what you need.

ChrisF
+1  A: 

DateTime.Parse()

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

marduk
+2  A: 
DateTime.Parse("Tuesday, March 30, 2010 10:45 AM")
Robin Day
+6  A: 

That's in the "F" format.

It should be parsed easily by

DateTime.Parse( s );

or by DateTime.ParseExact( string s, string format, IFormatProvider provider );

as

DateTime.ParseExact( s, "F", CultureInfo.InvariantCulture );
chris
+1 for ParseExact and CultureInfo, for a format like that I would say this is the most correct answer.
Chris Marisic
Figuring out what a FormatProvider was was one of the hardest things when starting out.
chris
+2  A: 

Parse may or may not work depending on your Culture settings.

I would recommend using the InvariantCulture, unless you can be sure your computer's culture is set to a culture that works ("en") and not one that fails ("ar").

DateTime.Parse("Tuesday, March 30, 2010 10:45 AM", CultureInfo.InvariantCulture )
Greg
+3  A: 

Not as simple but safer.

DateTime dts;
    string strDateTime = "Tuesday, March 30, 2010 10:45 AM";

if(!DateTime.tryParse(strDateTime, out dts))
     Console.WriteLine("not a date!");
Cheddar