Moment.js is a very, very popular date & time manipulation library in JavaScript. It has over 6 millions download. Each weeks. A baffling score.
And don't get me wrong, I use moment at work and we love it. But sometimes, beginners tend to use this overpopular library for any date manipulation, and even things that you can do in JavaScript without Moment.js.
Today I'm going to show you that you can save some bytes with this simple, yet powerful date formating in pure JavaScript.
Date, day, month
If I tell you that I want to display the day of the week (letters), the date, the month (letters) and the year, you may wanna rush into npm install moment
and use its format
method.
But you can do it in JavaScript. 0kb needed. Nada.
const date = new Date().toLocaleString("en-US", {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric"
});
console.log(date);
// Sunday, March 22, 2020
And it also supports other locales as well.
const date = new Date().toLocaleString("fr-FR", {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric"
});
console.log(date);
// dimanche 22 mars 2020
Cool, huh? Okay, the formatting may not be as versatile as in Moment, but this can be very useful to know that the global JavaScript Date object is able to do that.
Of course, Moment is great for some more complex formatting and date/time manipulation.
I hope that you learned something new in this tiny post. I won't cover in details what is available but you should definitely check out the toLocaleString method and other methods for the global Date object.
Alternatives mentioned by the community here on DEV:
Thanks to you, I learned that there is an entire repository giving awesome examples for your usual date/time manipulation operations (as well as the repositories mentioned above) here on you-probably-don-t-need-moment-js.
Top comments (13)
Heyn you can also use Humanize duration, he's lighter than moment.
npmjs.com/package/humanize-duration
Also day.js is quite light
Keep sharing your favorite date/time libraries! Thanks for that one.
I also found this one back in the day, It's done by one of moment's contributors:
github.com/moment/luxon
Yeep
I didn't know that one, I'll check it out. Thanks for sharing with us!
There is a whole GitHub repository where moment alternatives are compared!
That's awesome!
that's nice, but it cannot handle FUCKING TIMEZONES! these are the biggest PITA when it comes to dealing with datetime in js (and programming in general).
also, luxon is more like successor of moment.js, not alternative.
You are right, timezones are a pain, and this little trick is by no means a replacement for that purpose.
For now, I have no use case for timezone management in our application, and I think most beginners won't either. That's why I initially wrote this article. And it's really cool to have all of your opinions about that subject.
We are in the middle of a discussion with a colleague about whether or not we should/could change for Luxon, which seems really cool.
Thanks for sharing your thoughts!
Cool, I didn't know about this... cool stuff!
Please, everyone, stop using that crapton' heavy library already! :)
Having your page load decently is also a feature. ;)