Open In App

Angular 10 formatDate() Method

In this article, we are going to see what is formatDate in Angular 10 and how to use it. formatDate is used to format a date according to locale rules.

Syntax:



formatDate(value, locale, format, timezone)

Parameters:

Return Value:



NgModule: Module used by formatDate is:

Approach: 

import { LOCALE_ID, NgModule } from '@angular/core';

Example 1:




import {
  formatDate
 }
  from '@angular/common';
  
import {Component,
  Inject,
  LOCALE_ID }
  from '@angular/core';
  
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
curr = formatDate("02-february-0202", 'dd-MM-yyyy' ,this.locale);
constructor(
  @Inject(LOCALE_ID) public locale: string,){}
}




<h1>
  GeeksforGeeks
</h1>
  
<p>Locale Date is : {{curr}}</p>

Output:

Example 2:




import {
  formatDate
 }
  from '@angular/common';
  
import {Component,
  Inject,
  LOCALE_ID }
  from '@angular/core';
  
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
curr = formatDate("03-march-0303", 'yyyy-dd-MM' ,this.locale);
constructor(
  @Inject(LOCALE_ID) public locale: string,){}
}




<h1>
  GeeksforGeeks
</h1>
  
<p>Locale Date is : {{curr}}</p>

Output:

Reference: https://angular.io/api/common/formatDate


Article Tags :