VBA减去日期和时间

时间:2012-06-28 21:56:42

标签: excel vba date

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")

如何以“yyyy / mm / dd hh:mm:ss”格式减去两个日期?我尝试了上述两种方法,但都没有工作

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

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

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