EDDYMENS

Published 2 years ago

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 😊 GitHub: We weren’t able to create the release for you. Make sure you have a valid tag. (Fixed)