Sign in to follow this  
Followers 0
Marc

Ddays - Difference of days

9 posts in this topic

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

It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites



nice


-Brett

Share this post


Link to post
Share on other sites

Glad you like it :D


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thanx, I'll test it!

Best regards

Thorbjørn

Share this post


Link to post
Share on other sites

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

Thanks

Thorbjørn

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0