How to change the time format to 24 hours? in c #

Literal four = new Literal(); string timeanddate; timeanddate = DateTime.UtcNow.ToString(); DateTime dt = new DateTime(); DateTime dt_calc = new DateTime(); dt = Convert.ToDateTime(timeanddate); dt_calc = dt.AddHours(3); four.Text = "3hr added and this gives>> " + dt_calc.ToString(); form1.Controls.Add(four); 

all it in am pm i want to work with 24hrs

+4
source share
3 answers

See this page for each way you might want to format DateTime .

Please note that you use "HH" for 24-hour time.

For example, if you want the format β€œ23:00:00” instead of β€œ11:00:00 PM”, you should use:

 string formatted = dt_calc.ToString("HH:mm:ss"); 

By the way, your initializing your DateTime values ​​with new DateTime() not needed.

+9
source

You have to change the current culture.

 System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(1053); string swedishTime = DateTime.Now.ToShortTimeString(); //24h format System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(1033); string englishTime = DateTime.Now.ToShortTimeString(); //am/pm format 
+2
source
  System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss" 
0
source

All Articles