If you are utilizing Angular, one way to manage date formatting is by importing either DatePipe or FormatDate into your component.ts file. For more information on the available date/time formats that can be used as parameters, please refer to the documentation.
Option 1: DatePipe
import { DatePipe } from '@angular/common';
export class SampleComponent implements OnInit {
constructor(private datePipe: DatePipe) {}
transformDate(date) {
return this.datePipe.transform(date, 'yyyy-MM-dd');
}
}
Remember to include DatePipe in the providers array of your module.
providers: [DatePipe]
Option 2: formatDate
import { formatDate } from '@angular/common';
export class SampleComponent implements OnInit {
constructor(@Inject(LOCALE_ID) private locale: string) {}
transformDate(date) {
return formatDate(date, 'yyyy-MM-dd', this.locale);
}
}