I am trying to verify that a C# string is compliant with XML Schema dateTime format. Looking at the MSDN, it seems like "o", "s", or "u" standard format strings could all describe valid dateTimes, but I can't get DateTime.ParseExact to work for me. What am I doing wrong here?
string myDate = "1999-05-31T13:20:00.000-04:00";
DateTime.ParseExact(myDate, "o", CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime.ParseExact(myDate, "s", CultureInfo.InvariantCulture, DateTimeStyles.None);
DateTime.ParseExact(myDate, "u", CultureInfo.InvariantCulture, DateTimeStyles.None);
None of the above work. Sorry if my formatting is bad: first time posting a question here.
TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.
ToString("yyyy-MM-dd HH:mm:ss");
Lexical form The lexical form of xs:dateTime is a finite-length sequence of characters of the following form: yyyy - mm - dd T hh : mm : ss . ssssssssssss zzzzzz . The following abbreviations describe this form: yyyy. A four-digit numeral that represents the year.
Since you want to test that the data is XML compliant, you could use the XmlConvert.ToDateTime
method:
DateTime dt = XmlConvert.ToDateTime(myDate);
This will throw a FormatException
if the given string does not have the correct format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With