Search code examples
dateangularangular-pipedate-pipe

Format date as dd/MM/yyyy using pipes


I'm using the date pipe to format my date, but I just can't get the exact format I want without a workaround. Am I understanding pipes wrongly or is just not possible?

//our root app component
import {Component} from 'angular2/core'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <h3>{{date | date: 'ddMMyyyy'}}, should be 
      {{date | date: 'dd'}}/{{date | date:'MM'}}/{{date | date: 'yyyy'}}</h3>

    </div>
  `,
  directives: []
})
export class App {
  constructor() {
    this.name = 'Angular2'
    this.date = new Date();
  }
}

plnkr view


Solution

  • Pipe date format bug fixed in Angular 2.0.0-rc.2, this Pull Request.

    Now we can do the conventional way:

    {{valueDate | date: 'dd/MM/yyyy'}}
    

    Examples:

    Current Version:

    Example Angular 13


    Old Versions:

    Example Angular 8.x.x

    Example Angular 7.x

    Example Angular 6.x

    Example Angular 4.x

    Example Angular 2.x


    More info in documentation DatePipe