I need to parse ISO 8601 dates that does not contain a year and probably month component, such as:
--0412T102200Z
I am trying to use the following code which throws exception 'There must be at least a partial date with a year present in the input.':
DateTimeOffset.ParseExact("--0412T102200Z", "'--'MMdd'T'HHmmssZ",
CultureInfo.InvariantCulture, DateTimeStyles.None);
How do I parse such dates? Any chance to avoid replacing '--' with a year manually before parsing?
While substituting the year for dashes might be allowed in certain formats like XML XSD Schemas, that is technically not an ISO-8601 compliant value.
Ask yourself what this value means - it probably means "every year at this month, day, and time at UTC". In other words, it's a recurrence pattern.
The .Net
DateTime
andDateTimeOffset
types cannot store a recurrence pattern. They are for representing a specific date and time.So unfortunately, there is no way to parse this without a year. You will first need to determine which year you want to apply the pattern to, replace the dashes manually with
String.Replace
, and then you can parse it.