In many web applications, it is common to display dates and times in a human-friendly way. One way to achieve this is by formatting a moment object as a relative timestamp. For example, instead of displaying “2022-10-01T09:30:00Z”, we can display “2 hours ago”.

Moment.js is a popular JavaScript library for parsing, manipulating, and formatting dates and times. It provides various functions and methods to handle date-related tasks. One of the methods offered by Moment.js is fromNow(), which can be used to format a moment object as a relative timestamp.

Here is an example code snippet showcasing how to format a moment object as a relative timestamp:

const moment = require('moment');

// Current time
const currentTime = moment();

// A moment object representing a past date and time
const pastTime = moment('2022-10-01T09:30:00Z');

// Format the past time as a relative timestamp
const relativeTimestamp = pastTime.fromNow();

console.log(relativeTimestamp); // Outputs "2 hours ago"

In the example above, we:

  1. Import the Moment.js library using require('moment').
  2. Create a moment object representing the current time using moment().
  3. Create another moment object representing a past date and time by passing the desired date string to the moment() function.
  4. Format the past time as a relative timestamp using the fromNow() method.
  5. Display the relative timestamp using console.log().

By using Moment.js’s fromNow() method, we can easily format moment objects as relative timestamps such as “2 hours ago”, “2 days ago”, or “a month ago”. This can greatly enhance the user experience when displaying dates and times in web applications.

Remember to install Moment.js using a package manager like npm or yarn before using it in your project.

If you found this article helpful, share it with your friends and colleagues. #MomentJS #RelativeTimestamp.