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
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
 
    
    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
 
    
    Example as per doc
@Component({
  selector: 'date-pipe',
  template: `<div>
    <p>Today is {{today | date}}</p>
    <p>Or if you prefer, {{today | date:'fullDate'}}</p>
    <p>The time is {{today | date:'jmZ'}}</p>
  </div>`
})
export class DatePipeComponent {
  today: number = Date.now();
}
Template
{{ dateObj | date }}               // output is 'Jun 15, 2015'
{{ dateObj | date:'medium' }}      // output is 'Jun 15, 2015, 9:43:11 PM'
{{ dateObj | date:'shortTime' }}   // output is '9:43 PM'
{{ dateObj | date:'mmss' }}        // output is '43:11'
{{dateObj  | date: 'dd/MM/yyyy'}} // 15/06/2015
To Use in your component.
@Injectable()
import { DatePipe } from '@angular/common';
class MyService {
  constructor(private datePipe: DatePipe) {}
  transformDate(date) {
    this.datePipe.transform(myDate, 'yyyy-MM-dd'); //whatever format you need. 
  }
}
In your app.module.ts
providers: [DatePipe,...] 
all you have to do is use this service now.
 
    
    Try this below code it is also works well in angular 2
<span>{{current_date | date: 'yyyy-MM-dd'}}</span>
 
    
    Here is a very nice and compact way to do this, you can also change this function as your case needs:
result: 03.11.2017
//get date now function
    getNowDate() {
    //return string
    var returnDate = "";
    //get datetime now
    var today = new Date();
    //split
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //because January is 0! 
    var yyyy = today.getFullYear();
    //Interpolation date
    if (dd < 10) {
        returnDate += `0${dd}.`;
    } else {
        returnDate += `${dd}.`;
    }
    if (mm < 10) {
        returnDate += `0${mm}.`;
    } else {
        returnDate += `${mm}.`;
    }
    returnDate += yyyy;
    return returnDate;
}
 
    
    Add the template and give date pipe, you need to use escape characters for the format of the date. You can give any format as you want like 'MM-yyyy-dd' etc.
template: '{{ current_date | date: \'yyyy-MM-dd\' }}',
 
    
    You can import
import { formatDate } from '@angular/common';
The standard format of the method.
formatDate(value: string | number | Date, format: string, locale: string, timezone?: string)
So here, all you have to do is pass your "current_date" in the method as value, then format (in your case "YYYY-MM-DD") and then locale, timezone is optional.
e.g.
formatDate(current_date,'yyyy-MM-dd',"en-US");
You will get your desired output.
For more reference, you can look into Angular-formatDate
 
    
    Solution in ES6/TypeScript:
let d = new Date;
console.log(d, [`${d.getFullYear()}`, `0${d.getMonth()}`.substr(-2), `0${d.getDate()}`.substr(-2)].join("-"));
 
    
    Check date if valid "date instanceof Date" so you can convert as your format otherwise return same as
*Note:- We can use new Date() instead of moments
    convertIntoDate(value: string) {
     const date = new Date(value);
     if (date instanceof Date) {
       return moment(date, 'DD-MM-YYYY')
     } else {
       return value;
     }
    }