How to set date format in input of ng bootstrap

我们两清 提交于 2019-12-11 05:25:05

问题


I am using ng bootstrap datepicker. I want to set date format in input tag.
This is my html code

<input (click)="d.toggle()" type="text" [formControl]="updateEventForm.controls['live_date']" 
  value="{{dispalyLiveDate | date: 'd/M/yy'}}" ngbDatepicker #d="ngbDatepicker">

Here {{dispalyLiveDate | date: 'd/M/yy'}} is my default value, when select date then date format is convert to yyyy-M-dd, i want this date format same as d/M/yy
I had read document and find method format(date: NgbDateStruct) but don't know how to use it.
Is it possible to convert default date format direct into input tag(without convert into component) ?


回答1:


If you have the date with whatever format, you can paint it in the format that interests you in the html using this:

{{order.flight.date | date: 'dd/MM/yyyy'}} 

where 'order.flight.date' is the object where the date is stored.

It is important for this not to fail that the format is not the same that we want to obtain in any case.

I have not found a way to format the date to the desired format before storing it without doing it manually:

  // Analyze and change the date format
  parseDate(date) {
    let result = date;
    if (date != null) {
      if (date instanceof Date === false) {
        const parts = date.split('/');
        result = new Date(parts[2], parts[1] - 1, parts[0]);
      }
    }
    return result;
  }

The language i am using is Angular 5.

In ngx-bootstrap you can specify the format:

<input
        class="form-control"
        [minDate]="minDate"
        [maxDate]="maxDate"
        #dpYMD="bsDatepicker"
        bsDatepicker
        formControlName="myDateYMD"
        [bsConfig]="{ dateInputFormat: 'YYYY-MM-DD' }">


来源:https://stackoverflow.com/questions/42525190/how-to-set-date-format-in-input-of-ng-bootstrap

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