Given a DateTime object, how do I get an ISO 8601 date in string format?

Note to readers: Several commenters have pointed out some problems in this answer (related particularly to the first suggestion). Refer to the comments section for more information.

DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz", CultureInfo.InvariantCulture);

Using custom date-time formatting, this gives you a date similar to

Another way is:

DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);

which uses the standard “round-trip” style (ISO 8601) to give you

To get the specified format, you can use:

DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)

Leave a Comment