Search code examples
c#timeclocktimespanhour

C#, how do I make a clock work with added hours?


Using Visual Studio 2008 (C#) I have to make a working clock (digital) with the current time zone hour, and a few more with different time zones, like new york, etc.

inside the form I put 2 labels (for the clocks) and a timer, inside the timer I put this code:

 timer1.Interval = 1000;

        label1.Text = DateTime.Now.ToLongTimeString();
        DateTime myDateTime = DateTime.Now;


        TimeSpan myTimeSpan = new TimeSpan(2, 0, 0);
        DateTime myDateTime8 = myDateTime + myTimeSpan;
        label2.Text = ("" + myDateTime8);

the part with the timespan does add 2 hours to the clock, however, instead of just the actually clock I also get the date on it's left, like for example:

"17-05-2011 22:38:00"

I need to know how can I add/subtract hours and only show the clock.


Solution

  • Instead of adding a timespan, simply call the AddHours method:

    myDateTime.AddHours(2).ToLongTimeString();