如何减去从另一个日期时间日期时间?日期、时间

2023-09-02 10:33:03 作者:吃饭睡觉打豆豆╯

我如何提取两个日期时间值从另一个日期时间价值,有结果保存到一个双?

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

推荐答案

在.NET中,如果你从另一个减去一个的DateTime 的对象,你会得到一个时间跨度对象。然后,您可以使用该属性时间跨度对象拿到两日期时间的对象。由,不是双击

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;

有在时间跨度对象上的其他有趣的属性,如 TotalMilliseconds TotalMinutes 和类似的东西,可以帮助你,而且可能会更你要找的是什么

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.