date-fns - Modern JavaScript date utility library

Posted 1 month, 4 weeks ago
date-fns - Modern JavaScript date utility library

Why go all from scratch when you can just use the fully perfect tool? You can speed up your development work with ready use JavaScript date library, 'date-fns'. Let's have a look at how this library can help us to attempt something awesome within a lesser time.

First of all, go and have it in your local system with:

npm install date-fns

Now, you have it on your machine, go and do some simple steps to attach this awesome software to your project.

Import date-fns prerequisites with:

import { format, formatDistance, formatRelative, subDays } from 'date-fns'

Now, you've it inside your script. Get up, and use it like this:

format(new Date(), "'Today is a' eeee")
//=> "Today is a Sunday"

Congrats! You've just printed something is today on screen.

Now, come, and try something different, like this:

formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })
//=> "3 days ago"

Voila! This is what you bloggers, at least me blogger, always want. When designing my posts, I always wanted my timestamps to look like this. See, how amazing it showing how many days have passed since the post.

Also, you should try this:

formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 1:00 p.m."

See, how amazing is this library. I loved it a lot. You should try it for yourself. And, of course, let me know, whether you were using it already or will be from today.

Just go and give it a try.

Visit their official website here.

And, obviously, don't forget to share your opinion, experience, or anything when using this library. We're everywhere, ping us on Twitter, Facebook, Pinterest, or wherever you want.