Reputation: 1328
I have two dates in my project which are
let a="30-03-1999"
let b="30-03-2022"
Now i need to find the difference between these two dates, so first i am converting them to moment object by
let birthdate = moment(a, 'DD-MM-YYYY')
let licenseDate = moment(b, 'DD-MM-YYYY')
//console.log(birthdate,licenseDate) gives me moment object
Object { _isAMomentObject: true, _i: "30-03-1950", _f: "DD-MM-YYYY", _isUTC: false, _pf: {…}, _locale: {…}, _d: Date Thu Mar 30 1950 00:00:00 GMT+0530 (India Standard Time), _isValid: true }
upon running the function
var diff = licenseDate.diff(birthdate, 'years')
it returns NaN
Upvotes: 0
Views: 34
Reputation: 5522
DD-MM-YYYY
is not the correct date format, you should try with YYYY-MM-DD
, It will work fine
let a="1999-03-30"
let b="2022-03-30"
var date1 = moment(a);
var date2 = moment(b);
console.log(date1.diff(date2, 'years'))
and It will also with DD-MM-YYYY
let a="30-03-1999"
let b="30-03-2022"
var date1 = moment(a, 'DD-MM-YYYY');
var date2 = moment(b, 'DD-MM-YYYY');
console.log(date1.diff(date2, 'days'))
Upvotes: 1