Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dateTime to ISO format yyyy-mm-dd hh:mm:ss in C# [duplicate]

Tags:

c#

.net

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?

like image 664
Chin Avatar asked Dec 16 '09 07:12

Chin


People also ask

What is ParseExact C#?

ParseExact(String, String, IFormatProvider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.


1 Answers

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:

  • Standard Date and Time format strings
  • Custom Date and Time Format Strings
like image 133
Christian C. Salvadó Avatar answered Sep 21 '22 15:09

Christian C. Salvadó