Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change time format to 24hrs? in c#

Tags:

c#

datetime

    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);

its all in AM PM i want to work with 24hrs

like image 776
user287745 Avatar asked Aug 21 '10 07:08

user287745


1 Answers

See this page for every way you could possibly want to format a DateTime.

Note that you use "HH" for 24-hour time.

For example, if you wanted the format "23:00:00" instead of "11:00:00 PM" you would use:

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

By the way, your initialization of your DateTime values with new DateTime() is unnecessary.

like image 180
Dan Tao Avatar answered Sep 28 '22 08:09

Dan Tao