-3

how do i add a month to my date constant?

my variable receives an input with the date format I want to take this variable and each click add an extra month to it

I've tried using the gets and sets and nothing so far, like this : .getMonth()

I solved it, I created a function and added a stepUp that when I click on the button it adds days and it looks like this for example: function dateFunction() { document.getElementById("myDate").stepUp(30); }

Cleiton
  • 9
  • 5
  • 1
    Post a [mcve], `Date()` is tricky and we need to know to what exactly your variable is assigned to and the nature of your event handler (JavaScript and HTML). – zer00ne Jun 02 '23 at 18:15

3 Answers3

0

Here's a function that will do it for you.

function addMonth(date, numMonthsToAdd) {
  const newDate = new Date(date.setMonth(date.getMonth() + numMonthsToAdd));

  return newDate;
}
sumowrestler
  • 345
  • 5
  • 19
0

This should add 1 more month to you Date:

function addOneMonth(date) {
  const newDate = new Date(date.setMonth(date.getMonth() + 1));

  return newDate;
}
Jesus Fung
  • 76
  • 8
0

I feel getMonth should solve your problem assuming the input date is a date-type variable. Just wrote a sample code for the same.

let inputDate = new Date();

function addMonth(date) {
  const newDate = new Date(date.setMonth(date.getMonth() + 1));

  return newDate;
}

inputDate = addMonth(inputDate)

console.log('input date', inputDate)
Shraddha Goel
  • 869
  • 7
  • 18