获取 TimeSpan 中的总小时数 [英] Getting the total hours in a TimeSpan

查看:37
本文介绍了获取 TimeSpan 中的总小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何减去两个日期并获得返回的 TimeSpan 对象的总小时数?

How can I subtract two dates and get the total hours of the TimeSpan object that is returned?

例如,如果 TimeSpan 为 2 天,则总小时数为 48.

For example, if the TimeSpan is 2 days, the total hours are 48.

推荐答案

那么你想要 TotalHours 属性 TimeSpan 对象:

Then you want the TotalHours property of the TimeSpan object:

DateTime today = DateTime.Today;
DateTime twoDaysAgo = today.AddDays(-2.0);

// returns 48.0
double totalHours = (today - twoDaysAgo).TotalHours;

这篇关于获取 TimeSpan 中的总小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆