EDDYMENS

Last updated 2022-10-22 16:58:37

Get Human Readable Date In Javascript (Moment.js Format Time Ago)

You can present a human-friendly date to your readers using the moments.js [↗] library. This is an open-source Javascript library for working with dates.

The code sample below shows how to compare a given date, in this case, publishedDate to the current DateTime now and get back the duration in a human-readable format.

01: <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script> 02: <script> 03: var now = moment(); 04: var publishedDate = moment(new Date('2022/04/01')); 05: console.log(moment.duration(publishedDate.diff(now)).humanize(true)); // 7 months ago 06: </script>

Here is another article you might like 😊 "Diary Of Insights: A Documentation Of My Discoveries"