Getting time difference from now

We can use the following javascript code to get the date difference in readable format

e.g: Khalil posted a comment on ‘2024-05-10T01:25:14.317Z’ is less readable than Khalil posted a comment 7 hours ago.

I have used the library ( to achieve.

Sample Code

function getTimeAgo(dateStr) {
  const relativeTime = new RelativeTime(); // defaults to OS locale
  let dateObj = new Date(dateStr);
  return relativeTime.from(dateObj);

Code link github –

Live Application:

Live Application URL: