0

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

Pinak faldu
  • 79
  • 1
  • 7

1 Answers1

0

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");