setting up dates for future payments

I have a form from which users can schedule a series of payments; say, monthly for one year. When they hit the “Commit” button, 12 records are created through relationship to a child file. Having captured the begin date already, I want to mark each of these records with a date that is successively one month later than the previous. My thinking is to maintain the day of month unless to do so would fall outside of the number of days in that month, in which case the last day is recorded (example 2006/12/30 - 2007/01/30 - 2007/02/28 - 2007/03/30 etc.) Same would apply to scheduling payments in quarterly semiannual and annual bases.

I am thinking that it may be necessary to unpack the date into its year month and day components, then- in the payment record creation loop- increment and then repack into date format. Sounds like a whole lotta ‘if’ statements in there. Is there a better way? And how might I do the unpacking and repacking? I suspect that dateFormat is involved, but the documentation is a bit thin on this, at least to my way of thinking.

Suggestions greatly appreciated!

Jim

Maybe you can benefit from the tools plugin in this case:

getMaxDay - returns the last day of the given month

Ooh! Tastybite.

Thanks, Marcel!

Hi Jim,

Another approach: Some databases can work this out for you. Using PostgreSQL you can execute

SELECT '01-30-2006'::date + interval '1 month'

with databaseManager.getDatasetByQuery() and you will get
02-28-2006
:slight_smile:

That sounds great! It is a natural feature. We are not using PostgreSQL, however, and are- so far- keeping all of our coding in Servoy (in part because we do not know where we will end up, backend-wise).

I am inspired to ask Marcel if that functionality could be added to his Tools plug-in. Seems like a feature that should be available to all, as scheduling regular monthly payments is a reasonably common need. Or rather, “how much would it cost and how fast can we have it”.

Jim

Hello,

FWIW, I found this neat little function online:

// Method: getDaysInMonth(year, month)

var theYear  = arguments[0];
var theMonth = arguments[1];

return 32 - new Date(year, month, 32).getDate();

Marcel, maxDay doesn’t always work right. In setting up a test, I ran it from 2006.12.30 to 2007.02.28. When it got to February, the plugin somehow figured that 2007 was a leap year, plugged in “29” for the day of the month, and returned 2007.03.01 instead of 2007.02.28.

So I tried a variation on Sean’s function:

var monthNum = my_datefield.getMonth()
var yearNum = my_datefield.getFullYear()
var maxDay = 32 - new Date(yearNum, monthNum, 32).getDate();

This works great! It seems to work on the same principle as what Christian was saying that some back-ends do for one-month increments. Even better, it is all right there in Servoy.

Thanks folks,

Jim