how to convert current date to YYYY-MM-DD format with angular 2 how to convert current date to YYYY-MM-DD format with angular 2 angular angular

how to convert current date to YYYY-MM-DD format with angular 2


For Angular 5

app.module.ts

import {DatePipe} from '@angular/common';...providers: [DatePipe]

demo.component.ts

import { DatePipe } from '@angular/common';..constructor(private datePipe: DatePipe) {}ngOnInit() {   var date = new Date();   console.log(this.datePipe.transform(date,"yyyy-MM-dd")); //output : 2018-02-13}

more information angular/datePipe


Example as per doc

@Component({  selector: 'date-pipe',  template: `<div>    <p>Today is {{today | date}}</p>    <p>Or if you prefer, {{today | date:'fullDate'}}</p>    <p>The time is {{today | date:'jmZ'}}</p>  </div>`})export class DatePipeComponent {  today: number = Date.now();}

Template

{{ dateObj | date }}               // output is 'Jun 15, 2015'{{ dateObj | date:'medium' }}      // output is 'Jun 15, 2015, 9:43:11 PM'{{ dateObj | date:'shortTime' }}   // output is '9:43 PM'{{ dateObj | date:'mmss' }}        // output is '43:11'{{dateObj  | date: 'dd/MM/yyyy'}} // 15/06/2015

To Use in your component.

@Injectable()import { DatePipe } from '@angular/common';class MyService {  constructor(private datePipe: DatePipe) {}  transformDate(date) {    this.datePipe.transform(myDate, 'yyyy-MM-dd'); //whatever format you need.   }}

In your app.module.ts

providers: [DatePipe,...] 

all you have to do is use this service now.


Try this below code it is also works well in angular 2

<span>{{current_date | date: 'yyyy-MM-dd'}}</span>