使用 vb.net 从时间中减去分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20849679/
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
substract minutes from time using vb.net
提问by user3105451
I need to substract some minutes from a spesific hour and show the output. For example, substract 10 minutes from 14:50 and show the output as 14:40.
我需要从特定的小时中减去几分钟并显示输出。例如,从 14:50 减去 10 分钟并将输出显示为 14:40。
How can i do that? I have tried datediff function but It doesn't work for that I guess. I also tried date.substract but I couldn't solve the issue with that either.
我怎样才能做到这一点?我试过 datediff 函数,但我猜它不起作用。我也试过 date.substract 但我也无法解决这个问题。
Thanks in advance.
提前致谢。
回答by Kenneth
This should do the trick:
这应该可以解决问题:
Dim datetime As DateTime = Date.Now
Dim newdatetime As DateTime = datetime.Subtract(New TimeSpan(0, 10, 0))
Console.WriteLine(newdatetime.ToString("HH:mm"))
Or this:
或这个:
Dim datetime As DateTime = Date.Now
Dim newdatetime As DateTime = datetime.AddMinutes(-10)
Console.WriteLine(newdatetime.ToString("HH:mm"))
Or this:
或这个:
Dim datetime As DateTime = Date.Now
Dim newdatetime As DateTime = datetime.Subtract(TimeSpan.FromMinutes(10))
Console.WriteLine(newdatetime.ToString("HH:mm"))
The main point to take away is that if you subtract something from a date, the object itself is not updated, it just returns a new Date object with the result.
要带走的要点是,如果您从日期中减去某些内容,则对象本身不会更新,它只会返回一个带有结果的新 Date 对象。
回答by Ben
You need to use the timespan object.
您需要使用时间跨度对象。
Here is how to subtract 10 minutes from 14.50
这是从 14.50 减去 10 分钟的方法
Dim timeStart As New TimeSpan(14, 50, 0)
Dim timeToRemove As New TimeSpan(0, 10, 0)
Dim timeFinish As TimeSpan
timeFinish = timeStart.Subtract(timeToRemove)
Debug.Print(timeFinish.Hours.ToString + "-" + timeFinish.Minutes.ToString + "-" + timeFinish.Seconds.ToString)

