How to format DateTime to 24 hours time?

C#DatetimeFormat

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

Use upper-case HH for 24h format:

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

See DateTime.ToString Method.

Solution 2 - C#

Console.WriteLine(curr.ToString("HH:mm"));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYaraView Question on Stackoverflow
Solution 1 - C#FlashView Answer on Stackoverflow
Solution 2 - C#paulView Answer on Stackoverflow