[英]How to get inclusive total hours using TimeSpan in VB.NET
我在VB.NET中使用TimeSpan函数来获取计费月份的总小时数。
我在结算月份有30天,所以据说,我有720天。 通常使用Timespan,我会得到696个间隔。 但是我在“todate”中添加了一天,以便获得正确的总时数。 这是我的代码:
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #11-30-2014#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.AddDays(1).Subtract(fromdate)
totalhours = TS.TotalHours
但是,我不认为操纵我的输入日期(通过添加一天)是最好的做法。 有没有办法配置时间跨度功能,以获得包含总时数?
那是因为你没有提供时间范围。 时间部分的日期仅为上午12点。
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #11-30-2014 11:59:59 PM#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.Subtract(fromdate)
totalhours = TS.TotalHours
这将是1小时害羞。
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #12-01-2014#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.Subtract(fromdate)
totalhours = TS.TotalHours
将获得正确的金额,因为它包含两个日期之间的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.