如何使用VB.NET计算运行时间 [英] How to calculate running hour using VB.NET

查看:420
本文介绍了如何使用VB.NET计算运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算日期时间格式的两个日期之间的运行小时数(dd-MM-yy HH:mm)。例如,如果Onload datetime = 12-01-17 20:10 and offload datetime = 23-01-17 07:10



我尝试过:



How to calculate running hour between two dates in datetime format (dd-MM-yy HH:mm). eg if Onload datetime = 12-01-17 20:10 and offload datetime = 23-01-17 07:10

What I have tried:

Dim Date1 As Date
       Dim Date2 As Date
       Dim DateTime As TimeSpan
       Dim diff As Double

       Date1 = txtOnDT1.Value.ToShortDateString
       Date2 = txtOffDT1.Value.ToShortDateString
       DateTime = (Date1 - Date2)
       diff = DateTime.TotalDays

       MsgBox(Date1)
       MsgBox(Date2)
       MsgBox(diff)

推荐答案

尝试使用:

Try using:
diff = DateTime.TotalHours







我现在得到它 - 停止玩字符串:



[edit]
I get it now - stop playing with strings:

If Not DateTime.TryParse(txtOnDT1.Text, Date1) Then
	' REPORT PROBLEM TO USER
	Return
End If
If Not DateTime.TryParse(txtOffDT1.Text, Date2) Then
	' REPORT PROBLEM TO USER
	Return
End If
DateTime = (Date1 - Date2)
diff = DateTime.TotalHours

或者如果这些是DateTimePickers,则直接使用Value属性(在这种情况下,更改名称!)

[/ edit]

Or use the Value property directly if these are DateTimePickers (and in that case, change the names!)
[/edit]


这篇关于如何使用VB.NET计算运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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