Jump to content

Get the time difference from 2 times


damon
 Share

Recommended Posts

I am trying to take 2 times and determine the difference,

example:

Time one

13:43:24,424

Time two

13:44:35:424

Time that has passed is 0:1:11,000

how can i get the time that has passed?

 

It always amazes me how one little thing can cause so much havoc

Link to comment
Share on other sites

Something like this:

#include <date.au3>
Global $sTime1 = "13:43:24", $sTime2 = "13:44:35"
Global $iTime1,  $iTime2
Global $aTemp, $sHour, $sMinute, $sSecond
$aTemp = StringSplit($sTime1, ":")
$iTime1 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
$aTemp = StringSplit($sTime2, ":")
$iTime2 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
_TicksToTime($iTime2-$iTime1, $sHour, $sMinute, $sSecond)
ConsoleWrite($sHour & ":" & $sMinute & ":" & $sSecond & @LF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

 

Something like this:

That's almost the exact same code I came up with. :D Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

What's missing is some code to strip off the milliseconds from the input values. I will leave this to the OP ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

How would you do this for something that runs over midnight?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Then you would need to pass date AND time and use _DateDiff to calculate the difference.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Does the provided solution offer any benefits over:

$Start = "13:43:24:424"
$aStart = stringsplit($Start , ":")


$End = "13:44:21:300"
$aEnd = stringsplit($End , ":")


$Hour = $aEnd[1] - $aStart[1]
$Min = $aEnd[2] - $aStart[2]
$Sec = $aEnd[3] - $aStart[3]
$Ms = $aEnd[4] - $aStart[4]


If $Ms < 0 Then
$Sec -= 1
$Ms = 1000 + $Ms
Endif


If $Sec < 0 Then
$Min -= 1
$Sec = 60 + $Sec
Endif


If $Min < 0 Then
$Hour -= 1
$Min = 60 + $Min
Endif


msgbox(0, '' , $Hour & ":" & $Min & ":" & $Sec & ":" & $Ms)
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

If the result if the same I don't see a difference.

Both scripts should be tested if they return proper results when run over midnight.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I know i showed millisecs in the original post but I did not have to add it, so the first was great, i am rewriting it to better fit my script but that was exactly what i was trying to do.

 

thanks to all,

It always amazes me how one little thing can cause so much havoc

Link to comment
Share on other sites

oh, wow.  Water, i just caught on to what you were saying and tested it.  when it runs past the 24 hr mark it returns 12:1:11

so if i had a 23 hr time and 1hr comparison it will show something like 22 hrs difference instead of 1 - 2 hrs

Edited by damon

It always amazes me how one little thing can cause so much havoc

Link to comment
Share on other sites

To properly compare time values past the 24 hr mark you need to pass the date as well and use _DateDiff.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

What is the best practice for  "If <this>" or "If <this and that>" or " If <this and that and that>" if they all result in the same action, without copying the same stuff throughout the script (as that was the easiest solution)?

This one accounts for a single 24 hour period, with midnight rollover.

;;;;;; input

$Start = "22:43:24:424"

$End = "20:49:49:100"

;;;;;; end input

$aStart = stringsplit($Start , ":")
$aEnd = stringsplit($End , ":")

$Midnight = "23:59:59:999"
$aMidnight = stringsplit($Midnight , ":")

If $aEnd[1] < $aStart[1] Then
    $HourStart = $aMidnight[1] - $aStart[1]
    $MinStart = $aMidnight[2] - $aStart[2]
    $SecStart = $aMidnight[3] - $aStart[3]
    $MsStart = $aMidnight[4] - $aStart[4]
    $elapsedDay1 = _CheckTime($HourStart , $MinStart , $SecStart , $MsStart)

    $HourEnd = 0 + number($aEnd[1])
    $MinEnd = 0 + number($aEnd[2])
    $SecEnd = 0 + number($aEnd[3])
    $MsEnd = 0 + number($aEnd[4])
    $elapsedDay2 = _CheckTime($HourEnd , $MinEnd , $SecEnd , $MsEnd)

    $aDay1 = stringsplit($elapsedDay1 , ":")
    $aDay2 = stringsplit($elapsedDay2 , ":")

    $Hour = $aDay1[1] + $aDay2[1]
    $Min = $aDay1[2] + $aDay2[2]
    $Sec = $aDay1[3] + $aDay2[3]
    $Ms = $aDay1[4] + $aDay2[4]

ElseIf $aEnd[1] = $aStart[1] and $aEnd[2] < $aStart[2] Then
    $HourStart = $aMidnight[1] - $aStart[1]
    $MinStart = $aMidnight[2] - $aStart[2]
    $SecStart = $aMidnight[3] - $aStart[3]
    $MsStart = $aMidnight[4] - $aStart[4]
    $elapsedDay1 = _CheckTime($HourStart , $MinStart , $SecStart , $MsStart)

    $HourEnd = 0 + number($aEnd[1])
    $MinEnd = 0 + number($aEnd[2])
    $SecEnd = 0 + number($aEnd[3])
    $MsEnd = 0 + number($aEnd[4])
    $elapsedDay2 = _CheckTime($HourEnd , $MinEnd , $SecEnd , $MsEnd)

    $aDay1 = stringsplit($elapsedDay1 , ":")
    $aDay2 = stringsplit($elapsedDay2 , ":")

    $Hour = $aDay1[1] + $aDay2[1]
    $Min = $aDay1[2] + $aDay2[2]
    $Sec = $aDay1[3] + $aDay2[3]
    $Ms = $aDay1[4] + $aDay2[4]

ElseIf $aEnd[1] = $aStart[1] and $aEnd[2] = $aStart[2] and $aEnd[3] < $aStart[3] Then
    $HourStart = $aMidnight[1] - $aStart[1]
    $MinStart = $aMidnight[2] - $aStart[2]
    $SecStart = $aMidnight[3] - $aStart[3]
    $MsStart = $aMidnight[4] - $aStart[4]
    $elapsedDay1 = _CheckTime($HourStart , $MinStart , $SecStart , $MsStart)

    $HourEnd = 0 + number($aEnd[1])
    $MinEnd = 0 + number($aEnd[2])
    $SecEnd = 0 + number($aEnd[3])
    $MsEnd = 0 + number($aEnd[4])
    $elapsedDay2 = _CheckTime($HourEnd , $MinEnd , $SecEnd , $MsEnd)

    $aDay1 = stringsplit($elapsedDay1 , ":")
    $aDay2 = stringsplit($elapsedDay2 , ":")

    $Hour = $aDay1[1] + $aDay2[1]
    $Min = $aDay1[2] + $aDay2[2]
    $Sec = $aDay1[3] + $aDay2[3]
    $Ms = $aDay1[4] + $aDay2[4]

ElseIf $aEnd[1] = $aStart[1] and $aEnd[2] = $aStart[2] and $aEnd[3] = $aStart[3] and $aEnd[4] < $aStart[4] Then
    $HourStart = $aMidnight[1] - $aStart[1]
    $MinStart = $aMidnight[2] - $aStart[2]
    $SecStart = $aMidnight[3] - $aStart[3]
    $MsStart = $aMidnight[4] - $aStart[4]
    $elapsedDay1 = _CheckTime($HourStart , $MinStart , $SecStart , $MsStart)

    $HourEnd = 0 + number($aEnd[1])
    $MinEnd = 0 + number($aEnd[2])
    $SecEnd = 0 + number($aEnd[3])
    $MsEnd = 0 + number($aEnd[4])
    $elapsedDay2 = _CheckTime($HourEnd , $MinEnd , $SecEnd , $MsEnd)

    $aDay1 = stringsplit($elapsedDay1 , ":")
    $aDay2 = stringsplit($elapsedDay2 , ":")

    $Hour = $aDay1[1] + $aDay2[1]
    $Min = $aDay1[2] + $aDay2[2]
    $Sec = $aDay1[3] + $aDay2[3]
    $Ms = $aDay1[4] + $aDay2[4]


Else

    $Hour = $aEnd[1] - $aStart[1]
    $Min = $aEnd[2] - $aStart[2]
    $Sec = $aEnd[3] - $aStart[3]
    $Ms = $aEnd[4] - $aStart[4]

Endif


$Diff = _CheckTime($Hour , $Min, $Sec , $Ms)
$aDiff = stringsplit($Diff , ":")

    for $i = 1 to 4
        if StringLen($aDiff[$i]) < 2 Then $aDiff[$i] = "0" & $aDiff[$i]
    Next

msgbox(0, '' , $aDiff[1] & ":" & $aDiff[2] & ":" & $aDiff[3] & ":" & $aDiff[4])


Func _CheckTime($Hour , $Min , $Sec , $Ms)

If $Ms < 0 Then
    $Sec -= 1
    $Ms = 1000 + $Ms
ElseIf $Ms > 999 Then
    $Sec += 1
    $Ms = $Ms - 1000
Endif

If $Sec < 0 Then
    $Min -= 1
    $Sec = 60 + $Sec
ElseIf $Sec > 59 Then
    $Min += 1
    $Sec = $Sec - 60
Endif

If $Min < 0 Then
    $Hour -= 1
    $Min = 60 + $Min
ElseIf $Min > 59 Then
    $Hour += 1
    $Min = $Min - 60
Endif

return $Hour & ":" & $Min & ":" & $Sec & ":" & $Ms

EndFunc

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Modified my script to handle 24 hrs rollover as well:

#include <date.au3>
Global $sTime1 = "13:43:24", $sTime2 = "13:44:35"
Global $iTime1, $iTime2, $iTime24
Global $aTemp, $sHour, $sMinute, $sSecond
$aTemp = StringSplit($sTime1, ":")
$iTime1 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
$aTemp = StringSplit($sTime2, ":")
$iTime2 = _TimeToTicks($aTemp[1], $aTemp[2], $aTemp[3])
_TicksToTime($iTime2 - $iTime1, $sHour, $sMinute, $sSecond)
If Number(StringReplace($sTime1, ":", "")) < Number(StringReplace($sTime2, ":", "")) Then
    _TicksToTime($iTime2 - $iTime1, $sHour, $sMinute, $sSecond) ; No 24 hr rollover
Else
    $iTime24 = _TimeToTicks("24", "00", "00") ; 24 hr rollover
    _TicksToTime($iTime2 + $iTime24 - $iTime1, $sHour, $sMinute, $sSecond)
EndIf
ConsoleWrite(StringRight("0" & $sHour, 2) & ":" & StringRight("0" & $sMinute, 2) & ":" & StringRight("0" & $sSecond, 2) & @LF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

'?do=embed' frameborder='0' data-embedContent>>

#include <_DateDiff_2.au3>
#include <Array.au3>

Local $Old[6] = [2013, 08, 23, 13, 43, 24.240]
Local $New[6] = [2013, 08, 23, 13, 44, 35.925]
; Local $New[6] = [2013, 08, 23, 13, 43, 24.240]
; Local $Old[6] = [2013, 08, 23, 13, 44, 35.925]

$aAge = _DateDiff_2($Old, $New)
$s = '+'
If @extended Then $s = '-'
$s &= $aAge[0] & '/' & $aAge[1] & '/' & $aAge[2] & '  ' & $aAge[3] & ':' & $aAge[4] & ':' & $aAge[5] 
_ArrayDisplay($aAge, $s)
Edited by AZJIO
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...