install momentjs in angular 14

Install MomentJS in Angular 14

Let’s understand the method and steps to install momentjs in angular 14 in this tutorial. We will import and install it in a few simple steps. Before everything else we need to understand what Momentjs is.

Moment is a JavaScript library which is widely used by devs to parse, validate, manipulate and display date and time in JavaScript in a mannerly way. The display of date and time in Momentjs is in a formant to be read by humans easily.

This tutorial will get you through installing it in Angular 14 application in a few steps.

Install Moment JS

Type this command to install moment using node package manager.

npm install moment --save

Add Momentjs to Component ts file

The next we need to add Moment in Component ts file

src/app/app.component.ts

import { Component } from '@angular/core';
import * as moment from 'moment';
     
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'firstApp';
    
  constructor() {
    this.test();
  } 
  
  test() {
      const date = moment();
      let todayDate = date.format('M/D/YYYY');
      console.log(todayDate);
  }
}

Let’s Test it in Angular 14 Now

Since Moment js is already installed in Angular JS 14 now, it’s time to test it by running it.

ng serve

The next we will type http://localhost:4200 in our browser and test the date/time out of our app.

Output:

09/25/2022