I have a piece of code which parses and validates user input:
DateTime myDateTime = DateTime.Parse(userInput,currentCulture);
Current culture is being set (to en-ca or fr-ca) and the user Input is always in ISO 8601 format “yyyy-MM-dd”.
If the user enters 1900-01-01 the date is created as expected. If the input is “1900-01+01” the date time created is 1899-12-31 6:00:00 PM No exception is thrown, the DateTime.Parse happily converts this to the wrong date.
To make this work I am using DateTime.ParseExact(userInput,"yyyy-MM-dd",currentCulture).
So my question is not how to make this work (I have that) but whats up with the +01 or any + value? Am I missing something in ISO standard?
The only
+in ISO8601 is in time offset part, and it looks like it is parsed this way in this case. But as far as I know all 3 parts of date required to have valid value before time offset is allowed.I’d recommend using
DateTime.ParseExact(userInput,"yyyy-MM-dd",...(maybe even withInvariantCulture).DateTime.Parseaccepts huge variety of inputs and tries to make best guess on users intentions. This feels like a case when it simply guesses in confusing way.Sample values (first local PDT, 2 other with explicit time offset):
Which seem that
Parseessentially treats"YYYY-MM+0x"as"YYYY-MM-01T00:00+0x".