簡體   English   中英

Excel VBA datediff一起計算月,日和月?

[英]Excel VBA datediff to Calculate Months, Days and Month together?

我正在嘗試使用Datediff函數計算總月數,天數和小時數所用的時間。 這不可能嗎?

DateDiff("d hh", datein, Now)

我能做什么?

這是不可能的,因為interval參數只能是一個字符串。

你需要做更多的工作,比如獲得hours的差異,如果超過24,則將小數分隔符之前的部分轉換為天數

Sub Main()

    Dim d1 As Date
    d1 = "15/10/2014 08:00:03"

    Dim d2 As Date
    d2 = Now

    Dim hrsDiff As Long
    hrsDiff = DateDiff("h", d1, d2)

    MsgBox IIf(hrsDiff >= 24, _
               hrsDiff \ 24 & " days " & hrsDiff Mod 24 & " hours", _
               hrsDiff & " hours")

End Sub

這很粗糙,准備就緒,但只是方向性的。 您可以創建用戶定義的函數。 這個以字符串形式返回1:2:22:15(但您可以返回包含月,日,小時,分鍾變量的自定義類實例)。 它不考慮date2在date1之前(不確定會發生什么),也不考慮date1只是部分日(假設date1是午夜)。

Function MyDateDiff(date1 As Date, date2 As Date) As String
    Dim intMonths As Integer
    Dim datStartOfLastMonth As Date
    Dim datStartOfLastHour As Date
    Dim datEndOfMonth As Date
    Dim intDays As Integer
    Dim intHours As Integer
    Dim intMinutes As Integer
    Dim strResult As String
    ' Strip of any time
    datStartOfLastMonth = DateSerial(Year(date2), Month(date2), Day(date2))

    ' check the dates arent in the same month
    If Not ((Month(date1) = Month(date2) And Year(date1) = Year(date2))) Then

        ' how many months are there
        intMonths = DateDiff("m", date1, date2)
        Debug.Print (intMonths)

        ' how many days difference are there
        intDays = DateDiff("d", DateAdd("m", intMonths, date1), date2)
        Debug.Print (intDays)

        ' how many hours difference are there
        intHours = DateDiff("h", datStartOfLastMonth, date2)
        Debug.Print (intHours)

        ' how many minutes different are there
        datStartOfLastHour = datStartOfLastMonth + (DatePart("h", date2) / 24)
        intMinutes = DateDiff("n", datStartOfLastHour, date2)
        Debug.Print (intMinutes)
    Else
        ' Dates are in the same month
        intMonths = 0
        Debug.Print (intMonths)

        ' how many days difference are there
        intDays = DateDiff("d", date1, date2)
        Debug.Print (intDays)

        ' how many hours difference are there
        intHours = DateDiff("h", datStartOfLastMonth, date2)
        Debug.Print (intHours)

        ' how many minutes different are there
        datStartOfLastHour = datStartOfLastMonth + (DatePart("h", date2) / 24)
        intMinutes = DateDiff("n", datStartOfLastHour, date2)
        Debug.Print (intMinutes)
    End If

     strResult = intMonths & ":" & intDays & ":" & intHours & ":" & intMinutes
     MyDateDiff = strResult
End Function

測試這個:

?MyDateDiff(“01-SEP-2014”,“03-Oct-2014 22:15:33”)

得到:

1:2:22:15

即1個月,2天,22分15秒。

通過將組件添加回date1來反向測試它給出:

?使用DateAdd( “n” 個,15個,使用DateAdd( “H”,22,使用DateAdd( “d”,2,使用DateAdd( “M”,1 “01-SEP-2014”))))

=“03-Oct-2014 22:15:33”

如果我們在同一個月嘗試2個日期:

?MyDateDiff(“01-SEP-2014”,“03-SEP-2014 22:15:33”)

我們得到:

0:2:22:15

反向測試:

?使用DateAdd( “n” 個,15個,使用DateAdd( “H”,22,使用DateAdd( “d”,2,使用DateAdd( “M”,0, “01-SEP-2014”))))

得到:

03/09/2014 22:15:00

但是你可能想要考慮日期是錯誤的方式...你可能只想將date1計算為部分日期,如果它在當天晚些時候開始......正如我所說,只是一個想法。

問候

一世

這可能會給你一些想法來糾正2月或閏年的日子

Private Sub CommandButton1_Click()
    DoDateA
End Sub

Sub DoDateA()
    Dim D1 As Date, D2 As Date, DC As Date, DS As Date
    Dim CA: CA = Array("", "yyyy", "m", "d", "h", "n", "s", "s")
    Dim Va%(7), Da(7) As Date, Ci%
    D1 = Now + Rnd() * 420  ' vary the  * factors for range of dates
    D2 = Now + Rnd() * 156
    If D1 > D2 Then
        [b4] = "Larger"
    Else
        [b4] = " smaller"
        DS = D1
        D1 = D2
        D2 = DS
    End If
    [d4] = D1
    [e4] = D2
    DC = D2
    For Ci = 1 To 6
        Va(Ci) = DateDiff(CA(Ci), DC, D1)
        DC = DateAdd(CA(Ci), Va(Ci), DC)
        Va(Ci + 1) = DateDiff(CA(Ci + 1), DC, D1)
        If Va(Ci + 1) < 0 Then  ' added too much
            Va(Ci) = Va(Ci) - 1
            DC = DateAdd(CA(Ci), -1, DC)
            Cells(9, Ci + 3) = Va(Ci + 1)
            Cells(8, Ci + 3) = Format(DC, "yyyy:mm:dd hh:mm:ss")
        End If
        Da(Ci) = DC
        Cells(5, Ci + 3) = CA(Ci)
        Cells(6, Ci + 3) = Va(Ci)
        Cells(7, Ci + 3) = Format(Da(Ci), "yyyy:mm:dd hh:mm:ss")
        Cells(10, Ci + 3) = DateDiff(CA(Ci), D2, D1)
    Next Ci
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM