date format yyyy-MM-ddTHH:mm:ssZ

Viewed 288209

I assume this should be pretty simple, but could not get it :(. In this format Z is time zone.
T is long time pattern
How could I get a date in this format except by using

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm:ssZ"));

in C#

10 Answers

In C# 6+ you can use string interpolation and make this more terse:

$"{DateTime.UtcNow:s}Z"

Use:

DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffZ")

The outcome is : 2021-05-26T10:17:38.549Z

Related