38

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

user3237201
  • 459
  • 1
  • 4
  • 13

7 Answers7

65

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

Jayantha
  • 1,991
  • 1
  • 10
  • 12
  • 2
    I get this error: ERROR Error: InvalidPipeArgument: 'Missing locale data for the locale "de-DE".' for pipe 'DatePipe' – vahidsamimi Jun 05 '19 at 09:50
56

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.

Parth Ghiya
  • 6,699
  • 2
  • 29
  • 35
9

Try this below code it is also works well in angular 2

<span>{{current_date | date: 'yyyy-MM-dd'}}</span>
Ramesh Rajendran
  • 35,211
  • 40
  • 143
  • 222
6

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;
}
Avtandil Kavrelishvili
  • 1,491
  • 3
  • 25
  • 34
5

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\' }}',
Ani
  • 73
  • 1
  • 5
2

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

Sharayu Nalvade
  • 231
  • 2
  • 9
0

Solution in ES6/TypeScript:

let d = new Date;
console.log(d, [`${d.getFullYear()}`, `0${d.getMonth()}`.substr(-2), `0${d.getDate()}`.substr(-2)].join("-"));
Alex Fortuna
  • 1,203
  • 12
  • 15