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();
}
}
If anyone looking with time and timezone, this is for you
add z for time zone at the end of date and time format
You can achieve this using by a simple custom pipe.
Advantage of using a custom pipe is that, if you want to update the date format in future, you can go and update your custom pipe and it will reflect every where.
Custom Pipe examples
I think that it's because the locale is hardcoded into the
DatePipe
. See this link:And there is no way to update this locale by configuration right now.
You can also use momentjs for this sort of things. Momentjs is best at parse, validate, manipulate, and display dates in JavaScript.
I used this pipe from Urish, which works fine for me:
https://github.com/urish/angular2-moment/blob/master/src/DateFormatPipe.ts
In the args parameter you can put your date format like: "dd/mm/yyyy"
The only thing that worked for me was inspired from here: https://stackoverflow.com/a/35527407/2310544
For pure dd/MM/yyyy, this worked for me, with angular 2 beta 16:
I am using this Temporary Solution: