Displaying human friendly dates in your webapp

Views: 135   

Users will like human-friendly date strings like 2 months ago instead of a verbose date string like Thursday, April 11, 2019 4:40:24.494 PM

There are a couple of libraries in npm which helps doing this. Fromnow is one of my favourite library to achieve it.

Following are some of the examples of how to use the library to show human-friendly date strings.

fromNow(1555000824494, { max:1, suffix:true })
// => 2 months ago
fromNow(1555000824494, { max:2, suffix:true })
// => 2 months, 29 days ago
fromNow(1555000824494, { max:3, suffix:true })
// => 2 months, 29 days, 2 minutes ago
fromNow(new Date(), { max:3, suffix:true })
// => just now

You can also show future dates with this library, following are some of the examples

fromNow( '2025-01-01', { max:3, suffix:true })
// => 5 years, 5 months, 22 days from now
fromNow( '2020-01-01', { max:1, suffix:true })
// => fromNow( '2020-01-01', { max:1, suffix:true })

Hope this helps and makes you show better dates to your users.

On By