Karen from the Developer Relations team shows you how to install the Moment.js NPM package and walks through a few simple examples to show the differences between manipulating dates with JS vs Moment.
Have you worked with Moment before? What's your favorite reason to use Moment vs the JS date object?
Great contribution. Please allow me elaborate a bit, especially on the "why" (why would I use a date library?).
When you start working with JavaScript dates, you run into all kinds of scenarios, like you want to:
- add/subtract a couple of days from a given date
- check if 1 date is before/after the other
- find the earliest date given a bunch of dates
- check if one range of (2) dates does not overlap with another
- find every Monday of a certain month
- check if Feb 28 in a certain year exists
- convert a date (which also holds the time!) in Berlin to its equivalent in Tonga
-etc
Now, you can write all of these functions yourself in JavaScript. But why would you when others have already done that for you? And have put them all together in 1 package: a library in the form of an NPM.
"Moment" is such a library. I use "date-fns", just because it's lighter and actively developed. But both get the job done.