Spread the love

This post covers how to use Moment.js in Angular. We’ll go over why Moment.js is useful, how to install it, and how to use it in Angular to manipulate dates and times.

Moment.js is a powerful and flexible library for manipulating dates and times in JavaScript. It has been around since 2010 and has become the standard for date manipulation in JavaScript.

While JavaScript has a built-in Date object, it lacks a simple way to format dates and times and calculate relative dates. That’s where Moment.js comes in. It takes the pain out of working with dates and times in JavaScript.

Let’s start with simple installation, The first thing you need to do is install theMoment.js library. You can do this with simple steps using npm:

Add moment js in angular

npm install --save moment

Next, you need to import the library into your Angular component. You can do this using the import keyword:

Use moment js in angular

import * as moment from 'moment';

Once you’ve imported the library, you can start using it in your Angular component. The most common way to use Moment.js is to format dates and times. To do this, you use the format() method.

let myMoment: moment.Moment = moment("someDate");

The format() method takes a date or time as a string, and formats it according to the format string that you specify. complete example with component

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

We have covered simplu use the moment and you can learn more, check out the Moment.jsdocs.

Leave a Reply