How to format DateTime to 24 hours time?

Yara picture Yara · Mar 20, 2012 · Viewed 146.8k times · Source

I need string from datetime to display time in 24 hours format.

..
var curr = DateTime.Now;
string s = ???;
Console.WriteLine(s);
..

The output result have to be: "16:38" Thank you.

Answer

Flash picture Flash · Mar 20, 2012

Use upper-case HH for 24h format:

String s = curr.ToString("HH:mm");

See DateTime.ToString Method.