In my server, my dates are saved as a string in the ISO 8601 format. Example “2018-04-11T05:59:19.000Z”
How would I convert this string to a DateTime in c#?
In my server, my dates are saved as a string in the ISO 8601 format. Example “2018-04-11T05:59:19.000Z”
How would I convert this string to a DateTime in c#?
For things like this I would just google it.
“iso 8601 to datetime c#”
I actually did and tried the top answer on that very post (including the suggested amendment on the top answer) and found that I get errors.
public void TestConversion1 () {
string exampleDateISO = "2018-04-11T05:59:19.000Z";
DateTime dateFromISO = DateTime.Parse(exampleDateISO, null, System.Globalization.DateTimeStyles.RoundtripKind);
print(dateFromISO.ToLongTimeString());
}
public void TestConversion2 () {
string exampleDateISO = "2018-04-11T05:59:19.000Z";
DateTime dateFromISO = DateTime.Parse(exampleDateISO, null, System.Globalization.DateTimeStyles.RoundtripKind);
print(dateFromISO.ToLongTimeString());
}
I notice that my string has some extra digits but not exactly sure what I’m supposed to adapt to get the conversion to work? Thanks for your time!
Use a regex to format the string in a way that DateTime.Parse will accept. Or use a regex to just parse the whole thing yourself. If you’ve never used regular expressions, the topic is too big to teach in a forum response, but there are plenty of websites that teach them.