'how to find difference between two different formatted dates in year

My backend is sending me the date-time stamp of a document and I want to calculate the age of that document in frontend I am using moment but don't know how to calculate the difference between them

var TodaysDate = new Date();  //Mon May 09 2022 22:52:42 GMT+0530 (India Standard Time)
var DateFromServer = Entity;  // 2021-05-02T13:18:00.000Z

i want Answer in years like 3.4 years



Solution 1:[1]

With moment you can do it like this

const TodaysDate = moment('Mon May 09 2022 22:52:42 GMT+0530');
const DateFromServer = moment('2021-05-02T13:18:00.000Z');

const diff = TodaysDate.diff(DateFromServer, 'year', true);

console.log(diff.toFixed(2) + " Years");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anas Abdullah Al