I am trying to convert a string from String to DateTime but it gives me the following error:
System.FormatException: 'The string represents a DateTime not supported in the System.Globalization.GregorianCalendar calendar.'
This is what I'm trying
List<DateTime> fechas = new List<DateTime>();
fechas.Add(DateTime.ParseExact(strings[i], "yyyyMMddmmHHss", CultureInfo.InvariantCulture));
The type of value I pass to it is something like this: "20190601083158"
.
In the example you leave, the error makes sense.
If we read it in the following way, I think it will be much easier to notice
I think that seeing it this way, you will realize at a glance what is causing the error.
Within C#,
DateTime
the time in an object can range between 0 and 23, so trying to useDateTime.ParseExact
it throws a FormatException (since it is impossible to translate the number 31 into a valid time).Assuming that it was an error when typing the format string, and the penultimate value represents the minutes, we can parse it as follows:
Which would produce the following DateTime object
I typed it to avoid confusion over the representation of the date in different cultures