2

I would like to set the month and date of next month from the date of subscription.

Example: If today is 30th January, then the next date of subscription should change to the 30 days after 30th Jan means if the month is not leap then it should set to 2nd March instead of 30th Feb.

Along with this also when the month is December, the month should set to Jan.

This is the code I have thought to use which is solving my Dec to Jan problem but I am still unsure about the February thing.

var now = new Date();
if (now.getMonth() == 11) {
    var current = new Date(now.getFullYear() + 1, 0, now.getDate());
} else {
    var current = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate());
}
Biku7
  • 450
  • 6
  • 16
  • What you describe doesn't happen. You can either add add 1 month or add a fixed 30 days, the results are similar. 31 Jan + 1 month gives either 2 or 3 Mar depending whether it's a leap year or not. 31 Jan + 30 days gives 1 or 2 Mar (depending on leap year). – RobG Jan 14 '21 at 20:46

1 Answers1

0

adding 30 days to the current date will automatically get the next actual date after 30 days

the Date object will calculate the next date regardless of the number of days in the month

let someDate = new Date();
let numberOfDays = 30;

someDate.setDate(someDate.getDate() + numberOfDays); 
let dateFormated = someDate.toISOString().substr(0,10);
console.log(dateFormated);

someDate = new Date('2020-09-10');
someDate.setDate(someDate.getDate() + numberOfDays); 
dateFormated = someDate.toISOString().substr(0,10);
console.log(dateFormated);
Ahmed Gaafer
  • 1,603
  • 9
  • 26