Why date pipe doesn't work properly with ng2-bootstrap in Angular 2

北城余情 提交于 2019-12-13 07:22:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!