kevp
kevp

Reputation: 377

DateTime.TryParseExact and parsing multiple date formats

I am trying to see if a string is a valid date. I am having trouble since the dates are in "2011–7–17 23:18:52" format. I am testing like so:

String lstrTime = "2011–7–17 23:18:52";
DateTime datevalue; 
CultureInfo enUS = new CultureInfo("en-US");
if (DateTime.TryParseExact(lstrTime, "yyyy-MM-dd hh:mm:ss", enUS,
                     DateTimeStyles.None, out datevalue))
                        {
                            rtxtOutput.Text += "Valid datetime: " + lstrTime;
                        }

I think the reason I am having trouble is because the date can have only 1 M value or 2 MM for months. I have bulk data to process, and changing out the M to MM wont happen.

Edit: I have tried all suggested link, perhaps it could be the cultureinfo not recognizing a 24 hour clock(questionmark)

Upvotes: 0

Views: 3681

Answers (2)

JamieSee
JamieSee

Reputation: 13030

You need to use 24-hour time which is a capital H, "yyyy-MM-dd HH:mm:ss".

Upvotes: 3

Related Questions