在 vb.net 中获取两个时间跨度之间的时差 [英] Get time difference between two timespan in vb.net

查看:34
本文介绍了在 vb.net 中获取两个时间跨度之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个变量:

Dim starttime As TimeSpan
Dim endtime As TimeSpan

我的开始时间值是:02:30:00(我的意思是 2.30AM)
2.30AM 是第二天

My starttime value is : 02:30:00 (I mean 2.30AM)
2.30AM is next day

我的结束时间值是:10:30:00(我的意思是 10.30Am)

My endTime Value is : 10:30:00(I mean 10.30Am)

我想得到这些的时差.所以我有这样的代码:

I want to get time difference of these. So I have code like this:

Dim span3 As TimeSpan = starttime .Subtract(endtime ) 

现在我得到了 span3 : 08:00:00

Now I am getting span3 : 08:00:00

这是错误的答案.其实我想得到 16:00:00.(这是 2.30Am 到 10.30 Am 之间的确切差异)

This is wrong answer. Actually I want to get 16:00:00. (this is the exact differnce between 2.30Am To 10.30 Am)

如何计算?

推荐答案

您需要使用 DateTime 变量来保存开始时间和结束时间.像这样:

You need to use a DateTime variable to hold your start time and end time. Like this:

Dim startTime As New DateTime(2013, 9, 19, 10, 30, 0)     ' 10:30 AM today
Dim endTime As New DateTime(2013, 9, 20, 2, 0, 0)     ' 2:00 AM tomorrow

Dim duration As TimeSpan = endTime - startTime        'Subtract start time from end time

Console.WriteLine(duration)

结果:

15:30:00

更新:

要将结果转换为分钟,您可以使用 TimeSpan 变量的 TotalMinutes 属性:

To convert that result to minutes, you can use the TotalMinutes property of the TimeSpan variable:

Console.WriteLine(duration.TotalMinutes)

结果:

930

这篇关于在 vb.net 中获取两个时间跨度之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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