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 (https://github.com/yairEO/relative-time) 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 – https://github.com/khalilahmed232/khalilahmed232/tree/main/plain/date-time-ago

Live Application:

Live Application URL:

https://khalil232.com/apps/plain/date-time-ago/index.html