# Ddays - Difference of days

## Recommended Posts

Hi,

Since the version from Scriptkitty does not check for leap years and the

UDF announced here is not yet available, I wrote a function which calculates the number of days between two dates...

have fun!

Marc

```#include <date.au3>

\$a = ddays(20000101, 20050101)
msgBox(0, "Should be 1827", \$a)

\$a = ddays(20040101, 20050101)
msgBox(0, "Should be 366", \$a)

\$a = ddays(20030101, 20031231)
msgBox(0, "Should be 364", \$a)

\$a = ddays (19710604, 20040531)
MsgBox (0, "Should be 12050", \$a)

Func ddays(\$date1, \$date2)
\$moncalc = StringSplit("31,28,31,30,31,30,31,31,30,31,30,31", ",")
\$swap = 0
; date2 has to be the later date. If not, swap values
If \$date2 < \$date1 Then
\$temp = \$date1
\$date1 = \$date2
\$date2 = \$temp
\$swap = 1
EndIf

\$year1 = StringLeft(\$date1, 4)
\$month1 = StringMid(\$date1, 5, 2)
\$day1 = StringMid(\$date1, 7, 2)

\$year2 = StringLeft(\$date2, 4)
\$month2 = StringMid(\$date2, 5, 2)
\$day2 = StringMid(\$date2, 7, 2)

if (\$year1 = \$year2) and (\$month1 = \$month2) Then; same month in the same year? great!
\$difference = \$day2 - \$day1
Else; different months/years
\$difference = \$moncalc[\$month1] - \$day1; days until end of month in 1st date

;add one day for leap year in the first dates year
If _DateIsLeapYear(\$year1) and StringMid(\$date1, 5, 4) < "0228" and _
(\$year1<\$year2 or StringMid(\$date2, 5, 4) > "0229" ) Then \$difference = \$difference + 1

; if there is at least one month between the monthes of the 2 dates
; (Jan - Mar) is okay, but not (Jan-Feb)
If (\$month2 - \$month1 > 1) or (\$year1<>\$year2) Then
\$i = \$month1
\$j = 0
do
\$i = \$i + 1
\$difference = \$difference + \$moncalc[\$i]

If \$i = 12 Then; next year
\$i = 0
\$j = \$j + 1
; if complete year is covered and/or the range covers 29. February
If ((\$year1 + \$j) < \$year2) And _DateIsLeapYear(\$year1 + \$j) Then
\$difference = \$difference + 1
ElseIf _DateIsLeapYear(\$year1 + \$j) and StringMid(\$date2, 5, 4) > "0229" Then
\$difference = \$difference + 1
EndIf
EndIf
until (\$year1 + \$j = \$year2) and (\$i = \$month2 - 1); stops the beginning of targets month
EndIf
\$difference = \$difference + \$day2 ; add days in month of 2nd date
EndIf
if \$swap = 1 Then \$difference = -\$difference; if \$date2 < \$date1 return negative value
Return \$difference
EndFunc```

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

nice

-Brett
##### Share on other sites

nice

yeah. Very well, Marc.

@JLandes: Another function that might be useful for Time/Date functions.

##### Share on other sites

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

##### Share on other sites

• 4 weeks later...

Hi.. I really need this function, and it's great, but there's a bug in it. I tried using \$a = ddays(20041231, 20050101) as input.. well it craches..

It crashes if month number 12 is used in the first parameter (any day), but works fine if you use month 12 in the secound parameter instead.

Best regards

##### Share on other sites

• Developers

I have developed a bunch of date functions back in february to be included in the standard UDF library being compiled ... I believe they are still supposed to be published soon together with the AutoIt3 installer.

Here is a preview/beta version of these date functions in a file called http://www.autoitscript.com/fileman/users/jdeb/test/datenew.au3 .

It includes a _DateDiff function which does what you need:

```#include <datenew.au3>
msgbox(0,'test',_datediff('d',"2004/12/31", "2005/01/01"))```

Let me know when you find bugs or issues with it.

EDIT: this is the list of extra function on top of the current DATE.AU3:

; _FormatDateTime, _DayOfWeek, _DateIsValid

; _SplitDateTime, _Now(), _NowTime(), _NowDate(),_DaysInMonth()

; _DaysOfWeek(), Nowcalc(), _JulianDayNo(), _JulianToDate(\$iJDay)

Edited by JdeB

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Thanx, I'll test it!

Best regards

Thorbjørn

##### Share on other sites

Works great! Even has diff in time (this makes my job sooooo much easyer)

Thanks

Thorbjørn

##### Share on other sites

• 4 months later...

Thanks JdeB!

I was going to start writing my own versions of _DateAdd and _DateDiff but found this thread instead. I'm constantly amazed at how much you and the rest of the AutoIt community give of yourselves.

john925

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...