1

In asp.net Date.Now generates a complete date string: e.g "2018-01-17T11:12:42.0544453+00:00"

I am unable to validate this date string with the following formats. Can anyone suggest a suitable validation format?

Dim formats() As String = {
  "yyyy-MM-dd",
  "yyyy-MM-ddTHH:mm:ss",
  "yyyy-MM-ddTHH:mm:ss.fff",
  "yyyy-mm-ddThh:mm:ss.ffffff",
  "yyyy-mm-ddThh:mm:ss.fffffffZ",
  "yyyy-mm-ddThh:mm:ss.nnnnnnn",
  "yyyy-mm-ddThh:mm:ss.nnnnnnnZ",
  "yyyy-mm-ddThh:mm:ss.nnnnnnn+|-hh:mm"
}
If Date.TryParseExact("2018-01-17T11:12:42.0544453+00:00", formats, CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
  //Success
End If
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
DreamTeK
  • 32,537
  • 27
  • 112
  • 171
  • Even `Date.TryParse` would succeed, no need to use `ParseExact` – Tim Schmelter Jan 17 '18 at 12:07
  • @TimSchmelter I am using Parse exact because many different strings are checked, this is just an example of one that fails and I am curious why. – DreamTeK Jan 17 '18 at 12:19
  • If you're checking that it's valid, then `TryParse` is enough. You only need `TryParseExact` if you're ensuring it meets a given format. Which are you actually aiming for? – A Friend Jan 17 '18 at 12:27
  • @TimSchmelter I want to ensure the date is a valid ISO 8601 date format. This can be true even when time/fractions/timeszones are omitted. – DreamTeK Jan 17 '18 at 12:40
  • Possible duplicate of [How to create a .NET DateTime from ISO 8601 format](https://stackoverflow.com/questions/3556144/how-to-create-a-net-datetime-from-iso-8601-format) – A Friend Jan 17 '18 at 12:51
  • @AFriend This post is not a duplicate because I was unable to validate the string using the `Z` format. – DreamTeK Jan 17 '18 at 13:17

1 Answers1

1

I want to ensure the date is a valid ISO 8601 date format. This can be true even when time/fractions/timeszones are omitted.

You can use the "O"(Round-trip) format specifier

Dim time = "2018-01-17T11:12:42.0544453+00:00"
Dim validDate = Date.TryParseExact(time, "O", CultureInfo.InvariantCulture, DateTimeStyles.None, dt)

The "O" or "o" standard format specifier represents a custom date and time format string using a pattern that preserves time zone information and emits a result string that complies with ISO 8601. For DateTime values, this format specifier is designed to preserve date and time values along with the DateTime.Kind property in text. ...

Side-note: for me Date.Now.ToString doesn't return a string in this format. Maybe you have used:

Dim time = Date.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks for this Tim. Would this only validate if the timezone is present or is it more forgiving than explicit formats? Would it need to be an additional format string or will it cover several as those mention above? – DreamTeK Jan 17 '18 at 13:16
  • @Obsidian: it also parses `"2018-01-17T11:12:42.0544453"` successfully. But read the documentation i've linked. Especially the part at _"takes advantage of three ways that ISO 8601 represents time zone information: ..."_ It's also mentioned there in point three that it parses it if the _"date and time values have no time zone information"_ – Tim Schmelter Jan 17 '18 at 13:18
  • This is the magic. Thank you. I was aware of `o` and kind of embarassed I didn't try it. I got to bogged down with `s` and `z`. Shame there is no one universal validator for all ISO formats. – DreamTeK Jan 17 '18 at 13:30