在 vb.net 中获取两个时间跨度之间的时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18895045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:10:30  来源:igfitidea点击:

Get time difference between two timespan in vb.net

vb.net

提问by user2786971

I have two variables :

我有两个变量:

Dim starttime As TimeSpan
Dim endtime As TimeSpan

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

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

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

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

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

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

Dim span3 As TimeSpan = starttime .Subtract(endtime ) 

Now I am getting span3 : 08:00:00

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

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

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

How I can calculate this?

我如何计算这个?

回答by Chris Dunaway

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

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

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)

Result:

结果:

15:30:00

15:30:00

UPDATE:

更新:

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

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

Console.WriteLine(duration.TotalMinutes)

Result:

结果:

930

930

回答by NibblyPig

You're confusing DateTimeand TimeSpan. TimeSpanstores a duration, therefore anything about AM and PM is not relevant. If you want to compare two times, you should use DateTimeand subtract them both, which will give you a TimeSpan.

你很困惑DateTimeTimeSpanTimeSpan存储持续时间,因此任何有关 AM 和 PM 的内容都无关紧要。如果你想比较两次,你应该同时使用DateTime和减去它们,这会给你一个TimeSpan.

You cannot give a TimeSpan a value of '2am', you you should use DateTimefor that.

您不能为 TimeSpan 指定“2am”的值,您应该使用DateTime它。

Consider:

考虑:

var date1 = DateTime.Now.Date.AddHours(2); // pseudo code 2am
var date2 = DateTime.Now.Date.AddHours(11); // pseudo code 11am

var result = date2 - date1;

The result here is going to be a duration of 9 hours.

这里的结果将是 9 小时的持续时间。

If you want it to be 2am the following day, you should include AddDays(1);

如果你希望它是第二天凌晨 2 点,你应该包括 AddDays(1);

var date1 = DateTime.Now.Date.AddDays(1).AddHours(2); // pseudo code 2am the next day
var date2 = DateTime.Now.Date.AddHours(11); // pseudo code 11am

var result = date1 - date2;

The result here is going to be 15 hours.

这里的结果将是 15 小时。

回答by prasad chalasani

HERE IS MY SOLUTION FOR DIFFERENCE IN HOURS

这是我的小时差异解决方案

        'WORKS ONLY TIME SPAN WITHIN 48 HRS
        Dim HRS As TimeSpan
        Dim St As TimeSpan = TimeSpan.Parse(L_A_START.Text)
        Dim Cl As TimeSpan = TimeSpan.Parse(L_A_CLOSE.Text)

        HRS = Cl - St

        If HRS.Hours <= 0 Then
            HRS = (HRS + New TimeSpan(0, 24, 0, 0, 0))
        End If

        L_A_HRS.Text = HRS.ToString()