开发者

How can I convert this "Tuesday, March 30, 2010 10:45 AM" string to a DateTime practically?

开发者 https://www.devze.com 2022-12-25 03:09 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号