How do I get the time difference between two DateTime objects using C#?

前端 未结 9 2093
臣服心动
臣服心动 2020-11-27 11:38

How do I get the time difference between two DateTime objects using C#?

相关标签:
9条回答
  • 2020-11-27 12:01

    IF they are both UTC date-time values you can do TimeSpan diff = dateTime1 - dateTime2;

    Otherwise your chance of getting the correct answer in every single possible case is zero.

    0 讨论(0)
  • 2020-11-27 12:01

    You need to use a TimeSpan. Here is some sample code:

    TimeSpan sincelast = TimeSpan.FromTicks(DateTime.Now.Ticks - LastUpdate.Ticks);
    
    0 讨论(0)
  • 2020-11-27 12:05
    private void button1_Click(object sender, EventArgs e)
    {
        TimeSpan timespan;
        timespan = dateTimePicker2.Value - dateTimePicker1.Value;
        int timeDifference = timespan.Days;
        MessageBox.Show(timeDifference.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题