A challenge: adding months to a date?

David Bovill david at openpartnership.net
Sun May 13 14:06:52 EDT 2007


Sorry I thought it would be clear from the Use Case - basically I am not
interested in the days just the month number. The following function would
be more accurate:

on date_AddMonth monthsToAdd, startDate, @endMonth, @endYear
    -- add a whole number of months to a date returning the new dates month
and year
    ....
end date_AddMonth

The aim is simply to add one whole calendar month. The problem is when the
day of the start date is near the end of the month as convert can then take
this over to an extra month.... There seems to be a few quirks with convert
for dateitems - and I am updating to the latest version to see if this fixes
them. In the mean time your direct question looks like solving the problem
:) That is I an use the 1st of the month which will always work and then add
the days needed...

Anyway thats what I will try - the unit test has been modified to work
without Galaxy:

on mouseUp
    put "2002,1,31,1,0,0,5" into startDate
    put 1 into lastMonth
    put lastMonth into endMonths
    repeat with statementNumber = 1 to 14
        set the cursor to busy
        date_AddMonth statementNumber, startDate, endMonth, endYear
        put space & endMonth after endMonths

        if statementNumber mod 12 is not (endMonth - 1) then
            put merge("Failed adding [[statementNumber]] to
[[abbeyStartDate]]. Resulted in [[someMonth]]")
        end if
    end repeat

    put endMonths
end mouseUp

Should print out:

1 2 3 4 5 6 7 8 9 10 11 12 1 2 3

On 13/05/07, Ken Ray <kray at sonsothunder.com> wrote:


What would help would be what you expect to get as a result. Should the
> above conversion return the last day in February? So on a non-leap-year
> anything with a startdate of January 29, 30, or 31 should return
> February 28 when you add a month? Please clarify...


The Unit Test should print out:

2 3 4 5 6 7 8 9 10 11 12 1

as it ads 1 month to January 1, 2002



More information about the use-livecode mailing list