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

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

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

9条回答
  •  野性不改
    2020-11-27 11:53

    What you need is to use the DateTime classs Subtract method, which returns a TimeSpan.

    var dateOne = DateTime.Now;
    var dateTwo = DateTime.Now.AddMinutes(-5);
    var diff = dateTwo.Subtract(dateOne);
    var res = String.Format("{0}:{1}:{2}", diff.Hours,diff.Minutes,diff.Seconds));
    

提交回复
热议问题