skip to Main Content

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

3

Answers


  1. 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;
    }
    
    Login or Signup to reply.
  2. This should add 1 more month to you Date:

    function addOneMonth(date) {
      const newDate = new Date(date.setMonth(date.getMonth() + 1));
    
      return newDate;
    }
    
    Login or Signup to reply.
  3. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search