# Difference between 2 dates

## Recommended Posts

Hello, I have 2 dates in the format YYYY/MM/DD HH:MM

How can I calculate the difference in the same format?

##### Share on other sites

_DateDiff()

Monkey's are, like, natures humans.

##### Share on other sites

_DateDiff()

I wrote "in the same format".

With _DateDiff I can only get the difference in hour, mins, secs, etc.

##### Share on other sites

Plus more ...

D = Difference in days between the given dates

M = Difference in months between the given dates

Y = Difference in years between the given dates

w = Difference in Weeks between the given dates

h = Difference in hours between the given dates

n = Difference in minutes between the given dates

s = Difference in seconds between the given dates

But what do you exactly mean by same format? Could you provide an example of what you're intending your output to be? Because one can only presume that if you want to find the difference between two dates then _DateDiff would be the obvious choice. Edited by guinness
##### Share on other sites

Use _DateDiff and then format the result to your needs.

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

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

Tutorials:
WebDriver - Wiki

##### Share on other sites

Something like that?

```#include <Date.au3>

MsgBox(0, "Test",  Sec_2_Time_Format(_DateDiff("s", "2011/01/01 00:00:00", _NowCalc())))

Func Sec_2_Time_Format(\$iSec) ;coded by UEZ
Local \$days = 0
Local \$sec = Mod(\$iSec, 60)
Local \$min = Mod(Int(\$iSec / 60), 60)
Local \$hr = Int(\$iSec / 60 ^ 2)
If \$hr > 23 Then
\$days = Floor(\$hr / 24)
\$hr -= \$days * 24
EndIf
Return StringFormat("%01id %02i:%02i:%02i", \$days, \$hr, \$min, \$sec)
EndFunc   ;==>Sec_2_Time_Format```

Br,

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Example:

Date/Time = 2009/10/21 12:00

Current Date/Time = 2011/11/25 12:00

Difference = 2 years, 1 month, 4 days, 0 hours, 0 minutes

UEZ, you got the point but I don't understand why \$days = 0. Plus, years and months are missing.

It's a pity that AutoIT doesn't have a function to do this with a simple string...

##### Share on other sites

OK, your 'same format' was a little obscure, I would've worded it as '...output to ## Years ## Months ## Days ## Hours ## Minutes.'

Take a look at WinAPIEx.au3 by Yashied and look for the function _WinAPI_StrFromTimeInterval() for something 'inbuilt' into the Windows API.

Edited by guinness
##### Share on other sites

• Moderators

FSoft,

It's a pity that AutoIT doesn't have a function to do this with a simple string

Why should AutoIt have a builtin function when it is trivial to to this in a function you can write in a few seconds?

I take it this what you were looking for?

```#include <date.au3>

\$sEndDate = "2011/11/25 12:00"
\$sStartDate = "2009/10/21 12:00"

\$sDifference = _Date_Difference(\$sStartDate, \$sEndDate)
MsgBox(0, "Difference", \$sDifference)

Func _Date_Difference(\$sStartDate, \$sEndDate)

Local \$sReturn = ""

\$iYears = _DateDiff("Y", \$sStartDate, \$sEndDate)
\$sReturn &= \$iYears & "/"

; We now alter the start date to add the Years we have just found
; And now we look for the Months
\$iMonths = _DateDiff("M", \$sStartDate, \$sEndDate)
\$sReturn &= \$iMonths & "/"

; Now we add the Months
; And look for the Days
\$iDays = _DateDiff("D", \$sStartDate, \$sEndDate)
\$sReturn &= \$iDays & " "

; I am sure you get the idea......
\$iHours = _DateDiff("h", \$sStartDate, \$sEndDate)
\$sReturn &= \$iHours & ":"

\$iMins = _DateDiff("n", \$sStartDate, \$sEndDate)
\$sReturn &= \$iMins & ":"

\$iSecs = _DateDiff("s", \$sStartDate, \$sEndDate)
\$sReturn &= \$iSecs

Return \$sReturn

EndFunc```

All clear?

M23

Edit: Small typo.

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

Thought using EPOCH would be easier... well ...

```\$iTicks_Now = _Epoch_encrypt(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC)
\$iTicks_Than = _Epoch_encrypt(2009 & "/" & 10 & "/" & 21 & " " & 12 & ":" & 00 & ":" & 00)
ConsoleWrite(\$iTicks_Now - \$iTicks_Than & @TAB & \$iTicks_Now & @TAB & \$iTicks_Than & @CRLF)
\$iTicksDif_tmp = _EPOCH_decrypt(\$iTicks_Now - \$iTicks_Than)
\$aTicksDif_tmp = StringSplit(\$iTicksDif_tmp, "/")
\$aTicksDif_tmp[1] = StringFormat("%.2d",\$aTicksDif_tmp[1]-1970)
\$aTicksDif_tmp[2] = StringFormat("%.2d",\$aTicksDif_tmp[2]-1)
\$aTicksDif_tmp[3] = StringFormat("%.2d",Int(StringLeft(\$aTicksDif_tmp[3],2))-1)& StringTrimLeft(\$aTicksDif_tmp[3],2)
\$iTicks_Dif = \$aTicksDif_tmp[1] & "/" & \$aTicksDif_tmp[2] & "/" & \$aTicksDif_tmp[3]
MsgBox(0,"",\$iTicks_Dif)
Func _EPOCH_decrypt(\$iEpochTime)
; [url="http://www.autoitscript.com/forum/index.php?showtopic=83667&hl=epoch"]http://www.autoitscript.com/forum/index.php?showtopic=83667&hl=epoch[/url]
; trancexx
Local \$iDayToAdd = Int(\$iEpochTime / 86400)
Local \$iTimeVal = Mod(\$iEpochTime, 86400)
If \$iTimeVal < 0 Then
\$iTimeVal += 86400
EndIf
Local \$i_wFactor = Int((573371.75 + \$iDayToAdd) / 36524.25)
Local \$i_xFactor = Int(\$i_wFactor / 4)
Local \$i_bFactor = 2442113 + \$iDayToAdd + \$i_wFactor - \$i_xFactor
Local \$i_cFactor = Int((\$i_bFactor - 122.1) / 365.25)
Local \$i_dFactor = Int(365.25 * \$i_cFactor)
Local \$i_eFactor = Int((\$i_bFactor - \$i_dFactor) / 30.6001)
\$aDatePart[2] = \$i_bFactor - \$i_dFactor - Int(30.6001 * \$i_eFactor)
\$aDatePart[1] = \$i_eFactor - 1 - 12 * (\$i_eFactor - 2 > 11)
Local \$aTimePart[3]
\$aTimePart[0] = Int(\$iTimeVal / 3600)
\$iTimeVal = Mod(\$iTimeVal, 3600)
\$aTimePart[1] = Int(\$iTimeVal / 60)
\$aTimePart[2] = Mod(\$iTimeVal, 60)
EndFunc   ;==>_EPOCH_decrypt
Func _Epoch_encrypt(\$date)
Local \$main_split = StringSplit(\$date, " ")
If \$main_split[0] - 2 Then
Return SetError(1, 0, "") ; invalid time format
EndIf
Local \$asDatePart = StringSplit(\$main_split[1], "/")
Local \$asTimePart = StringSplit(\$main_split[2], ":")
If \$asDatePart[0] - 3 Or \$asTimePart[0] - 3 Then
Return SetError(1, 0, "") ; invalid time format
EndIf
If \$asDatePart[2] < 3 Then
\$asDatePart[2] += 12
\$asDatePart[1] -= 1
EndIf
Local \$i_aFactor = Int(\$asDatePart[1] / 100)
Local \$i_bFactor = Int(\$i_aFactor / 4)
Local \$i_cFactor = 2 - \$i_aFactor + \$i_bFactor
Local \$i_eFactor = Int(1461 * (\$asDatePart[1] + 4716) / 4)
Local \$i_fFactor = Int(153 * (\$asDatePart[2] + 1) / 5)
Local \$aDaysDiff = \$i_cFactor + \$asDatePart[3] + \$i_eFactor + \$i_fFactor - 2442112
Local \$iTimeDiff = \$asTimePart[1] * 3600 + \$asTimePart[2] * 60 + \$asTimePart[3]
Return SetError(0, 0, \$aDaysDiff * 86400 + \$iTimeDiff)
EndFunc   ;==>_Epoch_encrypt```
##### Share on other sites

• Moderators

I think this is about as good as I can get - or am prepared to go!:

```#include <Date.au3>

\$sEndDate = "2011/11/25 12:00"
\$sStartDate = "2009/10/21 12:00"

\$sDifference = _Date_Difference(\$sStartDate, \$sEndDate)
MsgBox(0, "Difference", \$sDifference)

Func _Date_Difference(\$sStartDate, \$sEndDate)

Local \$aUnit[6] = ["Y", "M", "D", "h", "n", "s"]
Local \$aInter[6] = ["/", "/", " ", ":", ":", ""]
Local \$sReturn = "", \$iUnit

For \$i = 0 To 5
\$iUnit = _DateDiff(\$aUnit[\$i], \$sStartDate, \$sEndDate)
\$sReturn &= \$iUnit & \$aInter[\$i]
Next

Return \$sReturn

EndFunc```

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

Here my extended version:

```#include <date.au3>

MsgBox(0, "Test",  Sec_2_Time_Format(_DateDiff("s", "2011/01/01 00:00", _NowCalc())))

Func Sec_2_Time_Format(\$iSec) ;coded by UEZ
Local \$years, \$months, \$days
Local \$sec = Mod(\$iSec, 60)
Local \$min = Mod(Int(\$iSec / 60), 60)
Local \$hr = Int(\$iSec / 60 ^ 2)
If \$hr > 23 Then
\$days = Floor(\$hr / 24)
\$hr -= \$days * 24
EndIf
\$years = Floor(\$days / 365)
\$days -= (\$years * 365)
\$months = Floor(\$days / 30.42)
\$days -= Floor(\$months * 30.42)
Return StringFormat("%02i years, %02i month, %02i days, %02i minutes, %02i hours, %02i seconds", \$years, \$months, \$days, \$hr, \$min, \$sec) ;y,m,d,h,m,s
EndFunc   ;==>Sec_2_Time_Format```

Br,

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

• 2 weeks later...

Thank you to everyone

##### Share on other sites

Oops, still a question.

With StringFormat is possible to omit the zeroed items?

E.g. if it's 0years, 1months, 0days it's possible to display only 1months?

##### Share on other sites

• Moderators

FSoft,

Just check you have a return from _DateDiff like this:

```#include <Date.au3>

\$sEndDate = "2011/11/25 13:00"
\$sStartDate = "2009/11/21 12:10"

\$sDifference = _Date_Difference(\$sStartDate, \$sEndDate)
MsgBox(0, "Difference", \$sDifference)

Func _Date_Difference(\$sStartDate, \$sEndDate)

Local \$aUnit[6] = ["Y", "M", "D", "h", "n", "s"]
Local \$aType[6] = ["years", "months", "days", "hours", "minutes", "seconds"]
Local \$sReturn = "", \$iUnit

For \$i = 0 To 5
\$iUnit = _DateDiff(\$aUnit[\$i], \$sStartDate, \$sEndDate)
If \$iUnit <> 0 Then
\$sReturn &= \$iUnit & " " & \$aType[\$i] & " "
EndIf
Next

Return \$sReturn

EndFunc```

Is that it?

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Thanks Belba

##### Share on other sites

Yes he's a great bloke that Belba

Double key skip typo, thats great

Monkey's are, like, natures humans.

##### Share on other sites

• Moderators

FSoft,

Pay no attention to those two. I am glad I could help.

B23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...