A challenge: adding months to a date?

David Bovill david at openpartnership.net
Sun May 13 14:18:25 EDT 2007


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


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

And prints out:

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



Does this work on other platforms? Note I had to  inset 2 into item 4 of the
dateItems to get it to work (the hours) which has never made any sense to me
- but is Revs version of midnight :) It would be nice to test it on other
versions and platforms as the convert to dateitems does seem a little
flakier than I thought?



More information about the use-livecode mailing list