如何在ng bootstrap输入中设置日期格式 [英] How to set date format in input of ng bootstrap

查看:423
本文介绍了如何在ng bootstrap输入中设置日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ng bootstrap datepicker .我想在输入标签中设置日期格式.
这是我的html代码

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">

下面是我的默认值,选择时的日期,然后日期格式转换为,我想这个日期格式相同点击 我已阅读文档并找到方法format(date: NgbDateStruct),但不知道如何使用它.
是否可以将默认日期格式直接转换为输入标签(不转换为组件)?

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) ?

推荐答案

如果您使用任何格式的日期,都可以使用html中您感兴趣的格式来绘制日期:

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'}} 

其中"order.flight.date"是存储日期的对象.

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;
  }

我使用的语言是Angular 5.

The language i am using is Angular 5.

在ngx-bootstrap中,您可以指定格式:

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' }">

这篇关于如何在ng bootstrap输入中设置日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆