angular get current date yyyy-mm-dd 1

angular get current date yyyy-mm-dd

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');
}

Here is what the above code is Doing:
1. We are importing the DatePipe from the @angular/common package.
2. We are adding the DatePipe to the providers array of the component.
3. We are creating a variable called myDate and assigning it a new Date object.
4. We are using the DatePipe to transform the myDate variable to the format we want.

Similar Posts