Not as flexible as Chris very complete solution but as accurate as it can be if the task is only to calculate with days :
DateCalc
sample use:
Code:
break on
"Date 7 Days before today: " + DateCalc(@date,-7)
get $
exit 0
;---- No changes beyond here ----
function DateCalc($date1, $DateOrMod, optional $SingleDigit)
dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1
dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2
$date1 = split($date1,'/')
if ubound($date1) <> 2
exit 1
endif
$_intYear1 = val($date1[0])
$_intMonth1 = val($date1[1])
$_intDay1 = val($date1[2])
if $_intMonth1 < 3
$_intMonth1 = $_intMonth1 + 12
$_intYear1 = $_intYear1 - 1
endif
$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +
$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306
select
case vartype($DateOrMod) = 3
$_intDate2 = $_intDate1 + $DateOrMod
if instr($_intDate2,'-') $_intDate2 = val(substr($_intDate2,2,len($_intDate2)-1)) endif
$_intYear2 = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
) + (100*($_intDate2+306)-25)
) / 36525
$_intMonth2 = ( 5 * ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) + 456
) / 153
$_intDay2 = ( ( ( 100*($_intDate2+306)-25)/3652425)
- ( ((100*($_intDate2+306)-25)/3652425)/4)
+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4
) - ( 153 * $_intMonth2 - 457
) / 5
if $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 endif
if not $SingleDigit
if len($_intYear2 ) < 4
$_ = execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")
endif
$_intMonth2 = right("0" + $_intMonth2,2)
$_intDay2 = right("0" + $_intDay2,2)
endif
$DateCalc = '' + $_intYear2 + '/' + $_intMonth2 + '/' + $_intDay2
case vartype($DateOrMod) = 8
$DateOrMod = split($DateOrMod,'/')
if ubound($DateOrMod) <> 2
exit 1
endif
$_intYear2 = val($DateOrMod[0])
$_intMonth2 = val($DateOrMod[1])
$_intDay2 = val($DateOrMod[2])
if $_intMonth2 < 3
$_intMonth2 = $_intMonth2 + 12
$_intYear2 = $_intYear2 - 1
endif
$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +
$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306
$DateCalc = $_intDate1 - $_intDate2
;comment the next line if you wish to return negative results also !!!
if instr($DateCalc,'-') $DateCalc = val(substr($DateCalc,2,len($DateCalc)-1)) endif
case 1
exit 1
endselect
endfunction
I am 36