get current date with 'yyyy-MM-dd' format in Angular 4
You can use DatePipe for formatting Date in Angular.
In ts if you want to format date then you can inject DatePipe as Service in constructor like this
import { DatePipe } from '@angular/common';@Component({ templateUrl: './name.component.html', styleUrls: ['./name.component.scss'], providers: [DatePipe]})myDate = new Date();constructor(private datePipe: DatePipe){ this.myDate = this.datePipe.transform(this.myDate, 'yyyy-MM-dd');}
And if you want to format in html file, 'Shortdate' will return date of type MM/DD/YY
{{myDate | date: 'shortDate' }}
As of Angular 6, this also works,
import {formatDate} from '@angular/common';formatDate(new Date(), 'yyyy/MM/dd', 'en');
you can try doing this.
component.ts
currentDate = new Date();
component.html
{{currentDate | date:'yyyy-MM-dd'}}