I am new to angular and looking to format date in component ngOnInit method. I have seen some example where pipe operator are used to format the data but i dont know how to format date in component file.
below is code
export class DashboardComponent implements OnInit {
formatdate = 'dd/MM/yyyy';
constructor(private auth: AuthService) { }
ngOnInit() {
console.log(new Date().toISOString())
}
}
You can find more information about the date pipe here, such as formats.
If you want to use it in your component, you can simply do
pipe = new DatePipe('en-US'); // Use your own locale
Now, you can simply use its transform method, which will be
const now = Date.now();
const myFormattedDate = this.pipe.transform(now, 'short');