i use this line to get the current date
public current_date=new Date();
and i have got this result:
Wed Apr 26 2017 10:38:12 GMT+0100 (Afr. centrale Ouest)
how can i transform that to this format
YYYY-MM-DD
We'll get the current date by using the date() method in the app. component. ts file. # Angular today = new Date();
You can format date/time in TypeScript, by using any of the built-in methods on the Date object or creating a reusable function that formats the date according to your requirements by leveraging built-in methods like getFullYear , getMonth , etc. Copied!
For Angular 5
app.module.ts
import {DatePipe} from '@angular/common'; . . . providers: [DatePipe]
demo.component.ts
import { DatePipe } from '@angular/common'; . . constructor(private datePipe: DatePipe) {} ngOnInit() { var date = new Date(); console.log(this.datePipe.transform(date,"yyyy-MM-dd")); //output : 2018-02-13 }
more information angular/datePipe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With