tags:

views:

27

answers:

2
    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

+4  A: 

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.

Dan Tao
okay but, is there a way to make the 24hrs default, that is what if i donot want to use any formatting options! just the value returned by default should be 24 hrs.
@user287745: Yes, there is! First see [jgauffin's answer](http://stackoverflow.com/questions/3536789/how-to-change-time-format-to-24hrs-in-c/3537366#3537366); then, for more information, see: [`CurrentCulture`](http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx) and [`DateTimeFormatInfo`](http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx).
Dan Tao
+1  A: 

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
jgauffin