User987 User987 - 1 year ago 103
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??

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download