Home > database >  How to calculate time difference (subtract time) in C#?
How to calculate time difference (subtract time) in C#?

Time:04-30

I am creating program to measure vacuum value with Arduino and display it in the form that created with C#. I want to store time as an constant. It is starting time of the program. I assigned it with "Connect" button. When I clicked, time value is storing. Then I am using "timer tick" method to see measured values instantly. Also, DateTime.Now shows me instant system time. It is changing like a clock. click here to see the picture

Here is the Connect button's code;

public void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;

        try
        {
            if (comboBox1.Text == "")

            {
                MessageBox.Show("Please select the port name!");
            }
            else
            {
                serialPort1.PortName = comboBox1.Text;
                serialPort1.ReadBufferSize = 8;
                serialPort1.Open();
                timeval.Clear();
                button1.Enabled = false;
                button2.Enabled = true;

                timer1.Start();

                DateTime myDateTime = DateTime.Now;  //It stores the instant time information when button is clicked.
                label14.Text = myDateTime.ToString(); // shows in the label

                
                //serialPort1.ReadTimeout = 300;

            }
          
        }
        catch (UnauthorizedAccessException)
        {
            MessageBox.Show("Unauthorized Access!");
        }
    }

Here is the timer tick's code;

   public void timer1_Tick(object sender, EventArgs e)
    {
        label12.Text = DateTime.Now.ToString();

        //TimeSpan time_difference = DateTime.Now - myDateTime; // trying to calculate time difference.

        //double saniye = time_difference.Seconds;

        //double dakika = time_difference.Minutes;

        //label10.Text = (Math.Round(saniye)).ToString();
        //label16.Text = (Math.Round(dakika)).ToString();

        new_data = 756 * (float.Parse(data) - 1023) / 1023;
        sensorval.Add(Math.Round(new_data, 1));
        all_data.Add(Math.Round(new_data, 1));
        textBox1.Text = Convert.ToString(Math.Round(new_data, 2));
        all_data.Sort();
        var peak_vacuum = all_data[0];
        textBox4.Text = peak_vacuum.ToString();

        if (sensorval.Count % 100 == 0)
        {
            sensorval.Sort();
            var find_max = sensorval[0];
            var find_min = sensorval[sensorval.Count - 1];
            textBox3.Text = find_min.ToString();
            textBox2.Text = find_max.ToString();
            sensorval.RemoveRange(0, 99);
        
        }

    }

I could not calculate time difference because myDateTime variable is calculating in button2 and defined in button2 method. But DateTime.Now is defined in timer tick method. So, I am getting an error that "The name 'myDateTime' does not exist in the current content." in the timer tick method. By the way, I tried to use counter in the timer tick to see the seconds after program works. It was not so accurate. It was slower then the real time. So, I choose above method. Thank you in advance.

CodePudding user response:

I thinks its like what you want.

 public partial class Form1 : Form
 {
    // accessable from all methods, events on Form1
    private DateTime myDateTime;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        myDateTime = DateTime.Now;
        // other codes
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // other codes
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // other codes
        var diffrenceSpan = DateTime.Now - myDateTime;

        var hours = diffrenceSpan.Hours;
        var minutes = diffrenceSpan.Minutes;   
        var seconds = diffrenceSpan.Seconds;
    }
}

gits project links

  • Related