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

About the Author

khalil

Hi, I'm Khalil, a passionate front-end developer with over 8 years of experience in web development. I specialize in building interactive, user-friendly websites using technologies like JavaScript, HTML, CSS, and Java.

I've had the opportunity to work on a variety of projects across different industries, helping businesses create modern and responsive web applications.

Outside of coding, I enjoy exploring the latest trends in design and tech, as well as sharing knowledge with others. I'm always looking for new challenges to grow my skills and make a positive impact in the tech community

You may also like these