46

Is there a standard way in .NET/C# to convert a datetime object to ISO 8601 format yyyy-mm-dd hh:mm:ss?

Or do I need to do some string manipulation to get the date string?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chin
  • 12,582
  • 38
  • 102
  • 152

6 Answers6

78

To use the strict ISO8601, you can use the s (Sortable) format string:

 myDate.ToString("s"); // example 2009-06-15T13:45:30

It's a short-hand to this custom format string:

myDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");

And of course, you can build your own custom format strings.

More info:

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
60

There is no standard format for the readable 8601 format. You can use a custom format:

theDate.ToString("yyyy-MM-dd HH':'mm':'ss")

(The standard format "s" will give you a "T" between the date and the time, not a space.)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Note that this will use `/` as date separator in places like the US and France (as in `2009/12/16 08:42:16`) – Fredrik Mörk Dec 16 '09 at 07:42
  • 6
    @Fredrik: No, it won't. If I would have used / in the format then it would have use the culture specific date separator, but - is a literal character and won't be replaced by anything else. – Guffa Dec 16 '09 at 08:00
  • @Guffa: you are right, of course. Mixed it up, my bad. – Fredrik Mörk Dec 16 '09 at 08:13
  • Nice one Guffa, I appreciate the help. – Chin Dec 16 '09 at 08:20
  • 1
    What about "u" nowadays? See DateTimeFormatInfo.UniversalSortableDateTimePattern Property – johv Jan 18 '13 at 16:16
  • @johv Apparently it adds a "Z" at the end too, to show that it's UTC. – johv Jan 18 '13 at 16:30
  • @johv: Yes, and interrestingly it does that even if the `DateTime` value isn't UTC.... – Guffa Jan 18 '13 at 16:32
  • Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer. – Guffa Mar 15 '13 at 23:38
  • 3
    This is incorrect. The standard requires a T when combining date and time. It can only be omitted when both parties agree. Using this directly with a browser will fail, for instance. Ref: https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations – oligofren Nov 29 '16 at 20:50
17

To add a little bit more information that confused me; I had always thought the same result could be achieved like so;

theDate.ToString("yyyy-MM-dd HH:mm:ss")

However, If your Current Culture doesn't use a colon(:) as the hour separator, and instead uses a full-stop(.) it could return as follow:

2009-06-15 13.45.30

Just wanted to add why the answer provided needs to be as it is;

theDate.ToString("yyyy-MM-dd HH':'mm':'ss")

:-)

Mick P
  • 204
  • 2
  • 4
10
date.ToString("o") // The Round-trip ("O", "o") Format Specifier
date.ToString("s") // The Sortable ("s") Format Specifier, conforming to ISO86801

MSDN Standard Date and Time Format Strings

oligofren
  • 20,744
  • 16
  • 93
  • 180
Luis
  • 167
  • 1
  • 5
0

For those who are using this format all the timme like me I did an extension method. I just wanted to share because I think it can be usefull to you.

     /// <summary>
    /// Convert a date to a human readable ISO datetime format. ie. 2012-12-12 23:01:12
    /// this method must be put in a static class. This will appear as an available function
    /// on every datetime objects if your static class namespace is declared.
    /// </summary>
    public static string ToIsoReadable(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd HH':'mm':'ss");
    }
codea
  • 1,439
  • 1
  • 17
  • 31
0

The DateTime::ToString() method has a string formatter that can be used to output datetime in any required format. See DateTime.ToString Method (String) for more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
A9S6
  • 6,575
  • 10
  • 50
  • 82