How can I convert this "Tuesday, March 30, 2010 10:45 AM" string to a DateTime practically?
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 simpl开发者_开发技巧e way to achieve this?
Thanks,
string strDateTime = "Tuesday, March 30, 2010 10:45 AM";
DateTime myDateTime = DateTime.Parse(strDateTime);
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 );
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!");
DateTime.Parse
or DateTime.ParseExact
should do what you need.
DateTime.Parse("Tuesday, March 30, 2010 10:45 AM")
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 )
DateTime.Parse()
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
精彩评论