以小时为单位显示两个日期时间值之间的差异 [英] Showing Difference between two datetime values in hours

查看:37
本文介绍了以小时为单位显示两个日期时间值之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中检索两个日期时间值.检索到值后,我需要两个值之间的差异.为此,我创建了一个时间跨度变量来存储 2 个日期值的差异.

I am retrieving two date time values from the database. Once the value is retrieved, I need the difference between the two values. For that, I create a timespan variable to store the difference of the 2 date values.

TimeSpan? variable = datevalue1 - datevalue2;

现在我需要显示存储在 Timespan 变量中的小时数差异.我提到了 TimeSpan.TotalHours 但由于某种原因无法应用.我怎么做?我在 MVC 项目中使用 C#.我只是需要以小时为单位显示差异值?

Now i need to show the difference which is stored in the Timespan variable in terms of number of hours. I referred to TimeSpan.TotalHours but couldn't apply the same for some reason. How do I do that? I am using C# on a MVC project. I simple need to show the difference value in hours?

由于时间跨度可为空,因此我无法使用总小时数属性.现在我可以通过执行 TimeSpanVal.Value.TotalHours;

推荐答案

我想你很困惑,因为你还没有声明 TimeSpan 你已经声明了 TimeSpan?代码> 这是一个 可空 时间跨度.如果您不需要它可以为空,请删除问号或使用 variable.Value.TotalHours.

I think you're confused because you haven't declared a TimeSpan you've declared a TimeSpan? which is a nullable TimeSpan. Either remove the question mark if you don't need it to be nullable or use variable.Value.TotalHours.

这篇关于以小时为单位显示两个日期时间值之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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