如何使用_DateDiff()来计算毫秒数?

时间:2017-08-06 11:50:25

标签: autoit datediff

我正在尝试使用AutoIt计算经过的时间(在Excel中两个单元格的日期值之间):

_DateDiff($sType, $sStartDate, $sEndDate)

我的问题是_DateDiff()不考虑毫秒。 它以秒为单位给出了经过的时间,但我希望它在几秒或几毫秒内完成。

1 个答案:

答案 0 :(得分:0)

  1. 你可以自己扩展这个功能。
  2. 您可以在* 1000函数之后计算您的值/ 1000

    ; #FUNCTION# ====================================================================================================================
    ; Author ........: Jos van der Zande
    ; Modified.......:
    ; ===============================================================================================================================
    Func _DateDiff($sType, $sStartDate, $sEndDate)
        ; Verify that $sType is Valid
        $sType = StringLeft($sType, 1)
        If StringInStr("d,m,y,w,h,n,s", $sType) = 0 Or $sType = "" Then
            Return SetError(1, 0, 0)
        EndIf
        ; Verify If StartDate is valid
        If Not _DateIsValid($sStartDate) Then
            Return SetError(2, 0, 0)
        EndIf
        ; Verify If EndDate is valid
        If Not _DateIsValid($sEndDate) Then
            Return SetError(3, 0, 0)
        EndIf
        Local $asStartDatePart[4], $asStartTimePart[4], $asEndDatePart[4], $asEndTimePart[4]
        ; split the StartDate and Time into arrays
        _DateTimeSplit($sStartDate, $asStartDatePart, $asStartTimePart)
        ; split the End  Date and time into arrays
        _DateTimeSplit($sEndDate, $asEndDatePart, $asEndTimePart)
        ; ====================================================
        ; Get the differens in days between the 2 dates
        Local $aDaysDiff = _DateToDayValue($asEndDatePart[1], $asEndDatePart[2], $asEndDatePart[3]) - _DateToDayValue($asStartDatePart[1], $asStartDatePart[2], $asStartDatePart[3])
        ; ====================================================
        Local $iTimeDiff, $iYearDiff, $iStartTimeInSecs, $iEndTimeInSecs
        ; Get the differens in Seconds between the 2 times when specified
        If $asStartTimePart[0] > 1 And $asEndTimePart[0] > 1 Then
            $iStartTimeInSecs = $asStartTimePart[1] * 3600 + $asStartTimePart[2] * 60 + $asStartTimePart[3]
            $iEndTimeInSecs = $asEndTimePart[1] * 3600 + $asEndTimePart[2] * 60 + $asEndTimePart[3]
            $iTimeDiff = $iEndTimeInSecs - $iStartTimeInSecs
            If $iTimeDiff < 0 Then
                $aDaysDiff = $aDaysDiff - 1
                $iTimeDiff = $iTimeDiff + 24 * 60 * 60
            EndIf
        Else
            $iTimeDiff = 0
        EndIf
        Select
            Case $sType = "d"
                Return $aDaysDiff
            Case $sType = "m"
                $iYearDiff = $asEndDatePart[1] - $asStartDatePart[1]
                Local $iMonthDiff = $asEndDatePart[2] - $asStartDatePart[2] + $iYearDiff * 12
                If $asEndDatePart[3] < $asStartDatePart[3] Then $iMonthDiff = $iMonthDiff - 1
                $iStartTimeInSecs = $asStartTimePart[1] * 3600 + $asStartTimePart[2] * 60 + $asStartTimePart[3]
                $iEndTimeInSecs = $asEndTimePart[1] * 3600 + $asEndTimePart[2] * 60 + $asEndTimePart[3]
                $iTimeDiff = $iEndTimeInSecs - $iStartTimeInSecs
                If $asEndDatePart[3] = $asStartDatePart[3] And $iTimeDiff < 0 Then $iMonthDiff = $iMonthDiff - 1
                Return $iMonthDiff
            Case $sType = "y"
                $iYearDiff = $asEndDatePart[1] - $asStartDatePart[1]
                If $asEndDatePart[2] < $asStartDatePart[2] Then $iYearDiff = $iYearDiff - 1
                If $asEndDatePart[2] = $asStartDatePart[2] And $asEndDatePart[3] < $asStartDatePart[3] Then $iYearDiff = $iYearDiff - 1
                $iStartTimeInSecs = $asStartTimePart[1] * 3600 + $asStartTimePart[2] * 60 + $asStartTimePart[3]
                $iEndTimeInSecs = $asEndTimePart[1] * 3600 + $asEndTimePart[2] * 60 + $asEndTimePart[3]
                $iTimeDiff = $iEndTimeInSecs - $iStartTimeInSecs
                If $asEndDatePart[2] = $asStartDatePart[2] And $asEndDatePart[3] = $asStartDatePart[3] And $iTimeDiff < 0 Then $iYearDiff = $iYearDiff - 1
                Return $iYearDiff
            Case $sType = "w"
                Return Int($aDaysDiff / 7)
            Case $sType = "h"
                Return $aDaysDiff * 24 + Int($iTimeDiff / 3600)
            Case $sType = "n"
                Return $aDaysDiff * 24 * 60 + Int($iTimeDiff / 60)
            Case $sType = "s"
                Return $aDaysDiff * 24 * 60 * 60 + $iTimeDiff
        EndSelect
    EndFunc   ;==>_DateDiff