How to Install Moment.js in Angular Projects
Edit 12/4/18: In response to some feedback I’ve gotten on this post, I decided to make a short screencast as an accompaniment. Please take a look below if you prefer watching to reading.
I have been working on a project that requires formatting dates and doing some light calculations to get the difference in days between two days.
There has been some digital ink spilled over the question of how to get Moment working in an Angular app. Curiously, I’ve found that a combination of the advice I found on StackOverflow and blogs worked. Perhaps it will for you too.
**note: I’m using Angular CLI. Also, because of the versioning differences, I can’t say if this solution will work for Angular 1.x. This is an Angular 2+ focused post.
1. Install Moment in Your App
npm install moment --save
2. Import Moment in the component TS file where you plan to use it
Most blog posts/StackOverflow answers leave this part out, but I found that Moment won’t work without it!
You can now use Moment.js in your Angular app, as long as you import it into the component in which you plan to use it. I hope this is helpful!
P.S. — Shoutout to this other post on the topic. I haven’t tried it yet but it seems like a good solution if this doesn’t work for you.