How to add the next date of subscription if the month is January from 29,30,31 to avoid the same date in February in javascript?

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());
}


Read more here: https://stackoverflow.com/questions/65715968/how-to-add-the-next-date-of-subscription-if-the-month-is-january-from-29-30-31-t

Content Attribution

This content was originally published by Biku7 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: