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");
}
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.
date.ToString("o") // The Round-trip ("O", "o") Format Specifier
date.ToString("s") // The Sortable ("s") Format Specifier, conforming to ISO86801
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:
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")
:-)
Source: Stackoverflow.com