In C#, given a DateTime object, how do I get a ISO8601 date in string format? – Stack Overflow
Posted by jpluimers on 2014/04/22
The first bulleted link below has been living in my drafts like forever (i.e. somewhere since mid June 2009), so time to write a bit about ISO 8601 and .NET.
First a few links about converting a DateTime into ISO 8601 string format:
- In C#, given a DateTime object, how do I get a ISO8601 date in string format? – Stack Overflow.
- datetime – Date format c# – Stack Overflow.
Some solutions use the “K” as a time zone specifier. At first, I couldn’t find any documentation for it, not even Google Search for Google Search for “ssK” DateTime ToString returns anything useful.
Later on, I found The “K” Custom Format Specifier in Custom Date and Time Format Strings.
So my preferred solutions for me are these:
System.DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssK");System.DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssK");
I avoid these:
System.DateTime.Now.ToString("o");
because it gets you too many digits in the second fracion.System.DateTime.Now.ToUniversalTime().ToString("s") + "Z";
because it is less clear what it does (might be resolved with a comment).
–jeroen
–jeroen






asdasd said
thanks.