问题
I want to show date in next format: dd/MM/yyyy
, but it only shows in MM/dd/yyyy
.
Here is my startdateComponent
:
import {Component, EventEmitter, Output, Input} from '@angular/core';
import {DATEPICKER_DIRECTIVES} from 'ng2-bootstrap/components/datepicker';
@Component({
templateUrl:'./app/home/item_component/start_date_component/start_date_component.html',
directives: [DATEPICKER_DIRECTIVES]
})
export class StartDateComponent () {
chosenDate : Date = new Date();
getDate() {
return this.chosenDate;
}
}
Template is:
<div class="col-sm-1 col-sm-offset-1 datePicked ">
<span>
<h4>{{getDate() | date: 'dd/MM/yyyy'}}</h4>
</span>
</div>
What is wrong here?
回答1:
I had the same question. Here is the answer, from the source:
* ## Usage
*
* expression | date[:format]
*
* where `expression` is a date object or a number (milliseconds
* since UTC epoch) or an ISO string
* (https://www.w3.org/TR/NOTE-datetime) and `format` indicates which
* date/time components to
* include:
...
* In javascript, only the components specified will be respected
* (not the ordering,
* punctuations, ...) and details of the formatting will be dependent
* on the locale.
So the answer is, "because format
does not work the way you expect it to work."
回答2:
I am currently working on new ng2-bootstrap datepicker and will expose moment.js powered date format, so you can use any format from: http://momentjs.com/docs/#/displaying/
PS if you want to get it asap and get updates, join https://www.hamsterpad.com/chat/ng2
来源:https://stackoverflow.com/questions/38352315/why-date-pipe-doesnt-work-properly-with-ng2-bootstrap-in-angular-2