How do I Print an Exception in Python?
April 29, 2024
Home >> Angular Q&A >> How to use Moment.JS in Angular?
If you are wondering how to use Moment.Js in Angular then you are at the right place. Moment.Js is a JavaScript package that makes it very simple to parse, validate, manipulate, and display date as well as time in JavaScript. Some developers prefer to use it because it allows them to display dates in human-readable format according to their current location.
Yes, you can use Moment in Angular, and there are two ways to install it in Angular depending on your project requirements.
It is one of the easiest methods of installing Moment.Js in your Angular project. Follow these steps to install it in your system.
npm install moment –save
import { Component } from '@angular/core';
import moment from 'moment';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
currentDate: moment.Moment = moment();
currentTime: string = moment().format('M/D/YYYY hh:mm:ss a');
format1: string = moment().format('M/D/YYYY');
format2: string = moment().format('MMMM D, YYYY');
format3: string = moment().format('YYYY-MM-DD hh:mm A');
futuredate =this.currentDate.add(1, 'days');
constructor() {
console.log(moment.locale()); // en
}
}
app.component.html
<h1>Current date and Time:<br />{{ currentDate }}</h1>
<h2>Format date 1<br />{{ format1 }}</h2>
<h2>Format date 2<br />{{ format2 }}</h2>
<h2>Format date 3<br />{{ format3 }}</h2>
<h2>Tomorrow's date:<br />{{ futuredate }}</h2>
You can simply include Moment.Js in your project through CDN (Content Delivery Network) if you don’t want to add Moment.Js as a dependency.
< script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js“>< /script >
Digital Valley, 423, Apple Square, beside Lajamni Chowk, Mota Varachha, Surat, Gujarat 394101
D-401, titanium city center, 100 feet anand nagar road, Ahmedabad-380015
+91 9913 808 2851133 Sampley Ln Leander, Texas, 78641
52 Godalming Avenue, wallington, London - SM6 8NW