A challenge: adding months to a date?
David Bovill
david at openpartnership.net
Mon May 14 07:40:07 EDT 2007
Thanks everyone!
And Ken - you've made my day by making me feel so stupid :) Seriously thanks
for the input. And in the mean time I've learned a lot about the quirks of
"convert" - tip is to ensure the time is 2am....
On 13/05/07, Ken Ray <kray at sonsothunder.com> wrote:
>
> 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
More information about the use-livecode
mailing list