Wednesday, 24 February 2010

CSharp: Dates in universal sortable format

Question:
How to format dates as sortable universal standard format strings in C#?




Answer:
You can use the "s" formating string in a datetime.ToString(s) overload.
European: 28/10/2010 1:30:22 PM -> 2010-10-28T01:30:22
Cf: Standard Date and Time Format Strings

Here is an example
DateTime dt = DateTime.Now;
string dtStr = dt.ToString("s");

No comments: