I'm a Nuxt newbie facing an issue with calculating the difference between two dates (user input date and current date). The code I am using works fine in most cases, but when the input date is '2020-03-31' or '2020-01-30', the console displays NaN years NaN month NaN day.
How can I resolve this? What mistake am I making?
I suspect that the months in the moment module start from 0-11 while my input months range from 1-12. Could someone please provide a sample code for obtaining the difference between 2 dates and outputting the year, month, and day?
Using module: https://www.npmjs.com/package/@nuxtjs/moment
methods: {
//example input format >>> date = '2020-08-21'
calcDate(date){
let nowDate = moment(new Date().toISOString().substr(0, 10).split('-'));
let pickDate = moment(date.toString().substr(0, 10).split('-'));
let dateDiff = moment.duration(nowDate.diff(pickDate));
console.log(dateDiff.years() + ' years ' + dateDiff.months() + ' month ' + dateDiff.days() + ' day ')
}
},
Thank you!