VBA 减去日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11253506/
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-08 13:25:10  来源:igfitidea点击:

VBA subtract date and times

excelvbadate

提问by user1384831

Dim dtmStart As Date, dtmEnd As Date, dblDuration As Double
dtmStart = "1900/01/01 08:10:00"
dtmEnd = "1900/01/03 21:16:00"
dblDuration = DateDiff("hh:mm", dtmEnd, dtmStart)
'dblDuration = dtmEnd - dtmStart
MsgBox Format(dblDuration, "hh:mm")

How would I subtract two dates in a "yyyy/mm/dd hh:mm:ss" format? I tried both methods above and neither work

如何以“yyyy/mm/dd hh:mm:ss”格式减去两个日期?我尝试了上面的两种方法,但都不起作用

回答by Tim Williams

Sub Test()
    Dim dtmStart As Date, dtmEnd As Date, dblDuration As Double
    dtmStart = "1900/01/01 08:10:00"
    dtmEnd = "1900/01/03 21:16:00"
    dblDuration = dtmEnd - dtmStart
    MsgBox Application.Text(dblDuration, "[hh]:mm")
End Sub

回答by Alex

Well I am a simple Dane, and I like it simple, so I just did it like this:

好吧,我是一个简单的丹麦人,我喜欢简单,所以我就是这样做的:

Dim dtmStart As Date, dtmEnd As Date, dblDuration As Date

Start:  'STARTTIME
        dtmStart = Time

Slut:           
        'SLUTTIDEN
        dtmEnd = Time
        dblDuration = dtmEnd - dtmStart
        Debug.Print "Modul 1 Slut  " & Time; " Run = "; dblDuration