A challenge: adding months to a date?
Björnke von Gierke
bvg at mac.com
Sun May 13 12:30:52 EDT 2007
I like challenges :)
The following function adds monthNums months, and gives you the last
day of that month, if the original day number was more then the target
month actually has days. Works on 2.8 (does NOT work correctly for
2.8.1 rc3 due to bug 4915)
function date_AddMonth monthNum, startDate
convert startDate to dateItems --does not work if startDate is a
system date
put item 3 of startDate into theDay
put 1 into item 3 of startDate
add monthNum to item 2 of startDate
convert startDate from dateItems to dateItems
put item 2 of startDate into theMonth
put item 1 of startDate into theYear
put getMaxDays(theMonth, theYear) into maxDay
if theDay > maxDay then
put maxDay into theDay
end if
put theDay into item 3 of startDate
--convert startDate from dateItems to <favorite date format>
return startDate
end date_AddMonth
function getMaxDays theMonth, theYear
put "1,January,3,March,5,May,7,July,8,August,10,October,12,December"
into Months31
put "4,April,6,June,9,September,11,November" into Months30
if theMonth is among the items of Months31 then
put 31 into HowManyDays
else if theMonth is among the items of Months30 then
put 30 into HowManyDays
else --February
if (theYear mod 400 = 0) or (theYear mod 100 <> 0) and (theYear mod
4 = 0) then
put 29 into HowManyDays -- leap year
else
put 28 into HowManyDays
end if
end if
return HowManyDays
end getMaxDays
--
official ChatRev page:
http://chatrev.bjoernke.com
Chat with other RunRev developers:
go stack URL "http://homepage.mac.com/bvg/chatrev1.3.rev"
More information about the use-livecode
mailing list