tags:

views:

142

answers:

4

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

+2  A: 

Try:

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

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

Reference: DateTime.ParseExact

OMG Ponies
the problem is i don't know what the user will enter.
newmem
Then use regexes to determine what format the date was provided in, and supply that format to the ParseExact.
OMG Ponies
A: 

"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.

Amber
+2  A: 

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)
...
SevenCentral
+2  A: 

if i understand your question

you can try with

ParseExact

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

anishmarokey