Jump to content

Ddays - Difference of days


Recommended Posts


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!


#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
   $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
            $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       
         until ($year1 + $j = $year2) and ($i = $month2 - 1); stops the beginning of targets month
      $difference = $difference + $day2 ; add days in month of 2nd date
   if $swap = 1 Then $difference = -$difference; if $date2 < $date1 return negative value
   Return $difference

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

Link to comment
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.

Can you fix it? Please?

Best regards

Thorbjørn Skistad

Link to comment
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:

; Added: _DateAdd, _DateDiff, _DateToDayValue, _DayValueToDate

; _FormatDateTime, _DayOfWeek, _DateIsValid

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

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

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
Live for the present,
Dream of the future,
Learn from the past.

Link to comment
Share on other sites

  • 4 months later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...