Pregunta

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)

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top