Search code examples
c#.netdatetimeiso8601

What is the date format of e.g. 2020-06-26T21:04:18.1823633Z called?


I’m using C# 4.6 to pass a datetime to a webservice that’s expecting it in a format like this: 2020-06-26T21:04:18.1823633Z, or yyyy-MM-ddTHH:mm:ss.fffffffZ.

On this website I’m finding formats that are similar (SortableDateTimePattern doesn’t have the Z and UniversalSortableDateTimePat­tern doesn’t have the T), but not identical. Is this a standard datetime format in C# and if not what would be the cleanest way to generate it?


Solution

  • It is the standard ISO-8601 format https://en.wikipedia.org/wiki/ISO_8601

    Most languages/libraries should be able to handle it as is, if not you may need to pluck the T or Z before processing.

    You can format your date with:

    MyDate.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
    //This will give you the time zone as say... -0400
    //2018-06-27T10:08:03.123456-0400
    //If you want it as Z (zulu) you can try
    MyDate.ToString("o");
    //2018-06-27T10:08:03.123456Z
    

    You can see all the formatting options here