Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert current date to YYYY-MM-DD format with angular 2

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

like image 659
user3237201 Avatar asked Apr 26 '17 09:04

user3237201


People also ask

How can I get current date in angular mm dd yyyy?

We'll get the current date by using the date() method in the app. component. ts file. # Angular today = new Date();

How do I change the format of a date in TypeScript?

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!


1 Answers

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

like image 113
Jayantha Avatar answered Sep 28 '22 21:09

Jayantha