-2

I am working on a program that calculates the difference between two given times. The difference between two times is calculated with button1 event handler and is displayed in label as shown in the windows form layout shown below.

This is my interface: enter image description here

My code:

I hope my question is clear. I tried my best but couldn't make it work. I just keep getting the wrong answer:

 public Form1()
        {
            InitializeComponent();
        }

        DateTime t1 = new DateTime(0, 11, 0);
        DateTime t2 = new DateTime(0, 16, 30);
        DateTime Dif = new DateTime(0, 0, 0);

        private void button1_Click(object sender, EventArgs e)
        {

            Dif = t1 - t2;
            Dif = Dif + DateTime.Parse(label1.Text);
            label1.Text = Dif.ToString();
        }
Amrit Sharma
  • 1,906
  • 8
  • 45
  • 75
Tacit
  • 890
  • 6
  • 17
  • 42
  • 1
    What are you getting and what are you expecting? – LukeHennerley Mar 12 '13 at 12:23
  • Shouldn't `t1 - t2` result in a `TimeSpan` object and shouldn't you get an error when trying to assign it to a `DateTime` object? And what do you expect to get from `new DateTime(0, 16, 30)`? – Corak Mar 12 '13 at 12:31

4 Answers4

2

You need to get time at two different times to see the notable difference, You may add two more button to start the time and end the timer. IMO StopWatch is more accurate for this purpose, start it on Start Button Click and end it on End button click.

Start stop watch

private void start_Click(object sender, EventArgs e)
{
    stopWatch.Start();
}

Stop StopWatch

private void end_Click(object sender, EventArgs e)
{
    stopWatch.Stop();
}

Here you will get the time interval

private void button1_Click(object sender, EventArgs e)
{
     TimeSpan ts = stopWatch.Elapsed;  //Here you will get the time interval         
     if(label1.Text != "")
     {
        TimeSpan tsOld =  TimeSpan.Parse(label1.Text);
        label1.Text = ts.Add(tsOld).ToString(); 
     }
     else
        label1.Text = ts.ToString(); 
}
Adil
  • 146,340
  • 25
  • 209
  • 204
1

It is not clear what are you trying to do, but you can't initialize a DateTime with that values.

Try with a TimeSpan

    TimeSpan t1 = new TimeSpan(0, 11, 0);
    TimeSpan t2 = new TimeSpan(0, 16, 30);
    TimeSpan Dif = (label1.Tag == null ? new TimeSpan(0, 0, 0) : label1.Tag as TimeSpan);

    Dif = Dif + (t2 - t1);

    label1.Text = Dif.ToString();
    label1.Tag = Dif;
Steve
  • 213,761
  • 22
  • 232
  • 286
  • But i need to keep increasing the label value by adding the new difference to the already calculated value – Tacit Mar 12 '13 at 12:34
0

I guess this should work:

 public Form1()
        {
            InitializeComponent();
        }

        TimeSpan t1 = new TimeSpan(0, 11, 0);
        TimeSpan t2 = new TimeSpan(0, 16, 30);

        private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan dif = t2 - t1;
            label1.Text = dif.ToString();
        }
andreasnico
  • 1,478
  • 14
  • 23
0

You only need to use the timeSpan type to subtract two dateTime! Here is a small example I did not compile this code:

DateTime TimeDate1 = DateTime.now;
Thread.Sleep(10000);
DateTime TimeDate2 = DateTime.now;
TimeSpan dateTimeResult = TimeDate2 - TimeDate1;

The result should be 10 seconds

Othar
  • 73
  • 1
  • 8
  • I dont think my question is understood correctly all i want to do is calulate the time difference of the times i have put on there and keep on adding that difference to the label by incrementing it by the same value – Tacit Mar 12 '13 at 12:39
  • Only enter your time in the DateTime value1 and value2. Use timeSpan value to subtract the two previous value – Othar Mar 12 '13 at 12:56