Akshay J
Akshay J

Reputation: 5468

ParseExact a string to DateTime fails

Nov 8 1:44

When I use this code, it fails:

string DD = "Nov 8 1:44";

try
{
    DateTime.ParseExact(DD, "MMM dd HH:mm", CultureInfo.InvariantCulture);
}
catch(Exception ex)
{
    MessageBox.Show("Bad day because " + ex.Message);
}

It says,

String was not recognized as a valid DateTime

Please let me know where am I wrong.

Upvotes: 3

Views: 142

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127593

That format would be

MMM d H:mm

per the MSDN you need a single d and H due to the fact that your source string is using a single digit day and hour.

"d"

The day of the month, from 1 through 31.

More information: The "d" Custom Format Specifier.

6/1/2009 1:45:30 PM -> 1

6/15/2009 1:45:30 PM -> 15


"dd"

The day of the month, from 01 through 31.

More information: The "dd" Custom Format Specifier.

6/1/2009 1:45:30 PM -> 01

6/15/2009 1:45:30 PM -> 15


"H"

The hour, using a 24-hour clock from 0 to 23.

More information: The "H" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 1

6/15/2009 1:45:30 PM -> 13


"HH"

The hour, using a 24-hour clock from 00 to 23.

More information: The "HH" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 01

6/15/2009 1:45:30 PM -> 13

Upvotes: 4

Related Questions