User987 User987 - 1 month ago 7
ASP.NET (C#) Question

Getting specific datetime format in C#

I need to get a specific time format in C# like this:

2016-010-31T00:00:00:000Z


I tried:

DateTime.Now.ToString("yyyy-MM-dd")


But this does not produces what I want...

How can I get the current datetime in this exact format like above??

Answer

Something like this?

string output = DateTime.Now.ToString("yyyy-0MM-ddTHH:mm:ss:fffZ");

I'm not sure about the "Z", if you wanted some time zone info. I zero padded the month like your example. Obviously there would never be a needed third decimal place.