User987 User987 - 3 months ago 30
ASP.NET (C#) Question

Getting specific datetime format in C#

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


I tried:


But this does not produces what I want...

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


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.