Question

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?

Was it helpful?

Solution

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 and DateTimeOffset 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top