Gang:

Here's a script that calculates the number of days between two dates. Maybe there's some snippets here that someone can use !

There must be a better ( faster ) way of doing this though ? Help me - but please don't make me look like an idiot for submitting this monster !

Plus - I think the code here has a minor ( one or two day diff) bug in it. Cudo's and admiration to anyone who can figure it out !


code:

; Calculate days between dates ...

break on


if $pipe
redirectoutput ( $pipe )
endif


dim $julian_runsum[13]


$julian_runsum[00] = 0
$julian_runsum[01] = 31
$julian_runsum[02] = 59
$julian_runsum[03] = 90
$julian_runsum[04] = 120
$julian_runsum[05] = 151
$julian_runsum[06] = 181
$julian_runsum[07] = 212
$julian_runsum[08] = 243
$julian_runsum[09] = 273
$julian_runsum[10] = 304
$julian_runsum[11] = 334
$julian_runsum[12] = 365


?"Enter date [YYYY/MM/DD] : " gets $date1
?"Enter date [YYYY/MM/DD] : " gets $date2


$y1 = val(substr($date1,1,4))
$m1 = val(substr($date1,6,2))
$d1 = val(substr($date1,9,2))


$y2 = val(substr($date2,1,4))
$m2 = val(substr($date2,6,2))
$d2 = val(substr($date2,9,2))


gosub the_days_between


?"Days between = $days"


exit

;----------------
:the_days_between
;----------------


$julian_y = $y1
$julian_m = $m1
$julian_d = $d1


gosub calc_julian


$t1 = $julian_date


$julian_y = $y2
$julian_m = $m2
$julian_d = $d2


gosub calc_julian


$t2 = $julian_date

if ( $y1 <> $y2 )


if ( $y1 > $y2 )


$max = $y1
$min = $y2


$julian_y = $y2
$julian_m = 12
$julian_d = 31


gosub calc_julian


$t2 = $julian_date - $t2


else


$max = $y2
$min = $y1


$julian_y = $y1
$julian_m = 12
$julian_d = 31


gosub calc_julian


$t1 = $julian_date - $t1


endif


$i = $max


while $i > ( $min + 1 )


$leapyear_y = $i


gosub isleapyear


$years = $years + 365 + $leapyear


$i = $i - 1


loop


else


$days = $t1 - $t2


return


endif


$days = $t1 + $t2 + $years


return


;-----------
:calc_julian
;-----------


; input : date as $julian_y $julian_m $julian_d
; output : days as $julian_date


$total = $julian_runsum [ $julian_m - 1 ] + $julian_d


if ( $julian_m > 2 )


$leapyear_y = $julian_y


gosub isleapyear


$total = $total + $leapyear


endif


$julian_date = $total


return


;----------
:isleapyear
;----------


if (( $leapyear_y * 4096 / 400 ) & (4095) ) = 0


$leapyear = 1


else


if (( $leapyear_y * 4096 / 4 ) & (4095) ) = 0 and (( $leapyear_y * 4096 / 100 ) & (4095) ) <> 0


$leapyear = 1


else


$leapyear = 0


endif


endif


return


Shawn.

[This message has been edited by Shawn (edited 08 June 2000).]