IMPORTANT ANNOUNCEMENT

These forums were permanently set to read-only mode on July 20, 2022. From that day onwards, no new posting or comment is allowed on the site, but the historical content remains intact and searchable.

A new location for posting questions about PlanetPress Suite is now available:

OL Learn - PlanetPress Classic (opens in new tab)

Topic Options
#57442 - 03/04/20 09:38 AM How do I add x months to calculated date in scrip?
Grzegorz Offline
OL Newbie

Registered: 01/24/20
Posts: 4
Loc: Skåne
HI
I need some help to finalize my script. This script is always setting up day to 28. If the give date is higher than 28, t. ex 29, 30, 31 - then month is changing (+1) and date is set to 28.
This works great. T. ex if date is 2020-03-03 I got 2020-03-28, and for 2020-03-29 - I got 2020-04-28

Then I need to calculate future date. And I have variable (cCrediTtime2) in months (can be 4, 15 or 60) and I have problem to calculate this new date.

Here is my code. Please help!!

var field = "";
field = record.fields["CreditTime2"];

var currentDate = new Date();
var currentDay = currentDate.getDate() ;
currentDate.setDate(28);

if (currentDay > 28) {
currentDate.setMonth(currentDate.getMonth() + 1);
}

var month = currentDate.getMonth() + 1 + field;
var year = currentDate.getFullYear();

results.html(year + '-' + month + '-' + 28);


[/i]

Top
#57443 - 03/04/20 02:35 PM Re: How do I add x months to calculated date in scrip? [Re: Grzegorz]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 681
Loc: Québec, Canada
currentDate.setMonth(currentDate.getMonth() + 1 + field);
var month = currentDate.getMonth();

by the way, you are in the wrong forum. this forum is for PlanetPress suite questions.

Next time, please post here


Edited by Jean-Cédric (03/04/20 02:36 PM)
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57445 - 03/09/20 09:11 AM Re: How do I add x months to calculated date in scrip? [Re: Grzegorz]
Grzegorz Offline
OL Newbie

Registered: 01/24/20
Posts: 4
Loc: Skåne
Thank you!. Just a final update. This is the working script for me:

var field = "";
field = record.fields["CreditTime2"];

var currentDate = new Date();
var currentDay = currentDate.getDate() ;
currentDate.setDate(28);

if (currentDay > 28) {
currentDate.setMonth(currentDate.getMonth() + 1);
}

//add number of months to already calculated date
currentDate.setMonth(currentDate.getMonth() + parseInt(field));
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();

results.html(year + '-' + month + '-' + 28);

Top