get current date with 'yyyy-MM-dd' format in Angular 4
How i can get the current date with a specific format 'yyyy-MM-dd', for today by example i with that the result be: '2018-07-12', with using just the command
myDate = new Date();
thanks a lot
3 answers
-
answered 2018-07-12 07:45
Peter
Here is the example:
function MethodName($scope) { $scope.date = new Date(); }
You can change the format in view here we have a code
<div ng-app ng-controller="MethodName"> My current date is {{date | date:'yyyy-MM-dd'}} . </div>
I hope it helps.
-
answered 2018-07-12 07:45
Krishna Rathore
You can use
date:'yyyy-MM-dd'
pipecurDate=new Date(); <p>{{curDate | date:'yyyy-MM-dd'}}</p>
-
answered 2018-07-12 07:50
khush
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
myDate = new Date(); constructor(private datePipe: DatePipe){ this.myDate = this.datePipe.format(this.myDate, 'yyyy-MM-dd'); }
And if you want to format in html file,
{{myDate | date:'yyyy-MM-dd'}}
As of Angular 6, this also works,
import {formatDate} from '@angular/common'; formatDate(new Date(), 'yyyy/MM/dd', 'en');