A challenge: adding months to a date?

Ken Ray kray at sonsothunder.com
Sun May 13 16:11:51 EDT 2007


On Sun, 13 May 2007 19:18:25 +0100, David Bovill wrote:

> This command passes the test:
> 
> on date_AddMonth monthsToAdd, startDate, @endMonth, @endYear
>    /*
>    Problem is if new month has less days in it than the original start date
> days
>    then just adding a month will add 2 months on conversion as the overlap
> pushes it over the month
> 
>    -- can add extra month if next months max days are shorter
>    -- not a bug a feature? Or could trucate day to last of month?
>    */
> 
>    convert startDate to dateItems
>    put startDate into someDate
> 
>    put item 3 of someDate into oDay
> 
>    put 1 into item 3 of someDate
>    put 2 into item 4 of someDate    # seems to be required !!!
>    add monthsToAdd to item 2 of someDate
>    convert someDate to dateItems
> 
>    put item 1 of someDate into endYear
>    put item 2 of someDate into endMonth
> 
>    put oDay into item 3 of someDate
>    convert someDate to internet date
>    return someDate
> end date_AddMonth

How about this:

on date_AddMonth monthsToAdd, startDate, @endMonth, @endYear
  put item 1 of startDate into tYear
  put item 2 of startDate into tMonth
  put (monthsToAdd+tMonth) mod 12 into endMonth
  if endMonth = 0 then put 12 into endMonth
  put ((monthsToAdd+tMonth) div 12) + tYear into endYear
end date_AddMonth


Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/



More information about the use-livecode mailing list