Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

How to add months to a date ? Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

How to add months to a date ?

Unread post by yvesf »

Hello,

I would like to add month to a date. I have got 2 dates in my edit form, the today's date and another date which should be 1 month after the date of the first chosen date. I know how to add days but I cannot find out how to add month.
I have tried that piece of code

Code: Select all

function AddMonthsToDateObject(i, months) {
 let 0 = $('#' + i);
let d = nuFORM.removeFormatting(o.val(), o.attr('data-nu-format'));
	nuSetDateValue(i ,new Date(d).addMonths(months));
}
It seems that addMonths() doesn't exist in Javascript. I have maybe to add months by using getMonth() and setMonth() but I haven't found yet. Anyone did that already ?
Many thanks

Yves
kev1n
nuBuilder Team
Posts: 4301
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: How to add months to a date ?

Unread post by kev1n »

yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: How to add months to a date ?

Unread post by yvesf »

Code: Select all

function AddMonthsToDateObject(i, months) {
 let 0 = $('#' + i);
let d = nuFORM.removeFormatting(o.val(), o.attr('data-nu-format'));
	nuSetDateValue(i ,new Date(d).setMonth(d.getMonth() + months));
}
AddMonthsToDateObject('date_field_to_add_month',1);
This function doesn't work, I don't understand why. Any idea ?
Last edited by yvesf on Sat Dec 24, 2022 1:50 am, edited 1 time in total.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: How to add months to a date ?

Unread post by yvesf »

It seems that getMonth() is not recognized as a Javascript function
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: How to add months to a date ?

Unread post by yvesf »

I have found the solution !!

Code: Select all

function addMonthsToDateObject(i, months) {
	let o = $('#' + i);
	let d = nuFORM.removeFormatting(o.val(), o.attr('data-nu-format'));  // remove format
const date = new Date(d); // creation new date based on d
date.setMonth(date.getMonth() + months); // the calcul is working when it's not done in a nuSetDateValue() function

nuSetDateValue(i , date); // you put date in the right field
}

addMonthsToDateObject('date_field_to_add_month',1);
Post Reply