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

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

问题描述

我从数据库中检索两个日期时间值。一旦检索到该值,我需要两个值之间的差值。
为此,我创建一个时间段变量来存储两个日期值的差异。

  TimeSpan? variable = datevalue1  -  datevalue2; 

现在我需要显示存储在Timespan变量中的差异,以小时数表示。
我参考了 TimeSpan.TotalHours ,但无法应用同样的原因
我该如何做?
我在MVC项目中使用C#。我简单地需要显示差异值在几小时?



编辑:
由于timespan可以空,我无法使用总时数属性。现在我可以用TimeSpanVal.Value.TotalHours来使用它了

解决方案

我觉得你很困惑,因为你没有声明一个 TimeSpan 你已经宣布了一个 TimeSpan?这是一个 nullable TimeSpan 。如果您不需要它可以为空,请使用 variable.Value.TotalHours


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;

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?

EDIT: Since timespan was nullable, i couldn't use the total hours property. Now I can use it by doing TimeSpanVal.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天全站免登陆