Marc Posted May 31, 2004 Posted May 31, 2004 Hi,Since the version from Scriptkitty does not check for leap years and theUDF announced here is not yet available, I wrote a function which calculates the number of days between two dates...have fun!Marcexpandcollapse popup#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)
Josbe Posted June 1, 2004 Posted June 1, 2004 niceyeah. Very well, Marc. @JLandes: Another function that might be useful for Time/Date functions. AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta
Marc Posted June 1, 2004 Author Posted June 1, 2004 Glad you like it Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)
t_bear Posted June 26, 2004 Posted June 26, 2004 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
Developers Jos Posted June 26, 2004 Developers Posted June 26, 2004 (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 June 26, 2004 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.
t_bear Posted June 26, 2004 Posted June 26, 2004 Works great! Even has diff in time (this makes my job sooooo much easyer) Thanks Thorbjørn
john925 Posted November 4, 2004 Posted November 4, 2004 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now