Getting Moment.js to Work with Angular 6 and Higher

One of the reasons I blog here is so that I'll have an easy way to find things I needed to figure out, so that I don't need to figure them out again. And I really do need to keep that in mind. Today, a co-worker of mine asked me for help getting Moment.js to work with an application using Angular 7. Before I could find a solution, he found one already in the code in a different area.

When I looked at the file in question, I'd written it (sigh). I didn't remember the problem (or the solution) until seeing the file again. At the time, I'd been trying to get Moment.js to work with Angular 6. Here's the simple solution:

//Moment doesn't want to behave in Angular 6. Here's the workaround.
import * as moment_ from 'moment';
const moment = moment_;

And then you just the use moment constant like this:

Example:
payload.birthDate = moment(profile.birthDate).toDate();

I think I probably found this solution on Stack Oveflow, so I can't really take credit for it, but I'm posting here so -- hopefully -- the next time this comes up, I'll remember it, and also so it can possibly help someone else in the future.

Comments

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Silent Renew and the "login_required" Error When Using oidc-client

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4