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?
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?
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:
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.)
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.