开发者

convert string to date in C#

How can I convert a date in a string to a DateTime type, even when the date does not conform to what the .net library supports.

"dec 2 2009"开发者_开发问答 
"dec 2, 2009"
"dec 2009"
"december 2009"
"dec. 2009"
"dec. 2, 2009"
and so on

is there a library for this?

assume us date format


Try:

MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

...or whatever the appropriate date format is...

Reference: DateTime.ParseExact


Check out the DateTime.ParseExact at http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx coupled with the custom formats at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx.

Here's an example:

...
DateTime.TryParseExact(s, "%M %d yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out resultDateTime)
...


if i understand your question

you can try with

ParseExact

E.g : DateTime.ParseExact(sDate, "mm/dd/yyyy", CultureInfo.InvariantCulture);


"even when the date does not conform to what the .net library supports."

"is there a library for this?"

So you want a library, for when the library doesn't support it?

Obviously, people have probably written other date/time string parsers before, but none of them is going to be exhaustive for anything you might ever possibly need - just find something that works with the formats you expect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜