1

Here is my Fiddle http://jsfiddle.net/p1kr0ayj/2/


function letsGo() {
  //for the inputs
  let name = document.querySelector('#inputName').value;
  let date = document.querySelector('#inputDate').value;
  let welcm = document.getElementById("welcome").innerHTML;
  let errorpopup = document.getElementById("error");
  let animation = document.getElementsByClassName("anim")[0];

  let currDate = moment().format("MM-DD-YYYY");
  console.log(currDate);
  let getDate = moment(date, "MM-DD-YYYY");
  if (getDate.isValid()) {
    //for the name
    welcm = "Welcome " + name;
    //calculating years
    yr = currDate.diff(getDate, 'years'); //heres the error
    getDate.add(yr, 'years');
    //calculating motnhs
    month = currDate.diff(getDate, 'months');
    getDate.add(month, 'months');

    console.log("nays", yr, month, currDate);
  } else {
    errorpopup.innerHTML = "Check Date";
    animation.classList.add('animError');
  }
  //console.log(getDate, errorpopup);
}

My target here is to get the total month difference not the total months since the year inputted. like the answer in the question below.

I tried to follow on what's on the documentation still gets this not a function error.

I tried to follow this Question Moment.js - Get difference in two birthdays in years, months and days

Still gets the error.

vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26
ravzz
  • 11
  • 1
  • Since you're using moment.js, just in case you've not seen https://momentjs.com/docs/#/-project-status, you may want to take this opportunity to stop using moment.js – Mike 'Pomax' Kamermans Sep 09 '21 at 20:49
  • 1
    `let currDate = moment().format("MM-DD-YYYY");` returns `currDate` as string, not an instance of moment, since it is formatted. Therefore you can't do `currDate.diff`. Do `moment(currDate).diff(getDate, 'months');` instead. – Timur Sep 09 '21 at 23:33
  • 1
    I somehow got it right and clean some redundant code. thanks sir @tim – ravzz Sep 10 '21 at 06:01

0 Answers0