How to subtract a datetime from another datetime?

后端 未结 7 932
借酒劲吻你
借酒劲吻你 2020-12-03 20:36

How do I subtract two DateTime values from another DateTime value and have the result saved to a double?

7条回答
  •  天涯浪人
    2020-12-03 21:08

    In .NET, if you subtract one DateTime object from another, you will get a TimeSpan object. You can then use the Ticks property on that TimeSpan object to get the number of ticks between the two DateTime objects. However, the ticks will be represented by a Long, not a Double.

    DateTime date1;
    DateTime date2;
    Long diffTicks = (date2 - date1).Ticks;
    

    There are other interesting properties on the TimeSpan object like TotalMilliseconds and TotalMinutes and things like that which can help you out, and may be more what you are looking for.

提交回复
热议问题