Sling Academy
Home/JavaScript/JavaScript: How to Convert Date Time to Time Ago

JavaScript: How to Convert Date Time to Time Ago

Last updated: February 19, 2023

Time ago is the concept of expressing a time relative to the current time. For example, if it is currently 3 pm and we want to talk about the moment at 1 pm, then we can say 2 hours ago. Time ago is widely used in modern applications like social media, news, etc., to convey how long ago an event happened in a friendly manner.

So, how do you convert date time to time ago in Javascript? Let’s find out in the example below:

// Define a function that takes a date as an argument
// and returns a string that represents how long ago the date was
export const timeAgo = (date) => {
  const seconds = Math.floor((new Date() - date) / 1000);

  let interval = Math.floor(seconds / 31536000);
  if (interval > 1) {
    return interval + ' years ago';
  }

  interval = Math.floor(seconds / 2592000);
  if (interval > 1) {
    return interval + ' months ago';
  }

  interval = Math.floor(seconds / 86400);
  if (interval > 1) {
    return interval + ' days ago';
  }

  interval = Math.floor(seconds / 3600);
  if (interval > 1) {
    return interval + ' hours ago';
  }

  interval = Math.floor(seconds / 60);
  if (interval > 1) {
    return interval + ' minutes ago';
  }

  if(seconds < 10) return 'just now';

  return Math.floor(seconds) + ' seconds ago';
};

// at the time of writing this code, the date is 2022-12-28 16:14:00
// you will see different results if you run this code at different time
console.log(timeAgo(new Date('2019-01-29 00:00:00')))
console.log(timeAgo(new Date('2022-10-24 19:00:00')))
console.log(timeAgo(Date.now() - 60 * 5 * 1000))

My output:

3 years ago
2 months ago
5 minutes ago

You can reuse the timeAgo function we’ve created above in your future projects so that you don’t have to rewrite all the code. It works nicely, and the job is done with only vanilla Javascript. No third-party libraries are necessary.

Next Article: JavaScript: Get current date time in yyyy/MM/dd HH:mm:ss format

Series: Date and Time in JavaScript: Basic to Advanced Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration