简体   繁体   English

ng-bootstrap datepicker 无法在 select 之前设置默认日期

[英]ng-bootstrap datepicker unable to set default date before select

I am using ng-bootstrap date picker.我正在使用 ng-bootstrap 日期选择器。 this is my html for datepicker input这是我的 html 用于日期选择器输入

<input type="text"
id="datepicker{{i}}"
class="form-control"
formControlName="dateJoined"
ngbDatepicker
#incorporatedDatePicker="ngbDatepicker"
(click)="incorporatedDatePicker.toggle()"
(dateSelect)="onDateSelection($event)"
readonly>

However i am unable to set a default date to this input field before i select from the datepicker itself.但是,我无法在日期选择器本身的 i select 之前为此输入字段设置默认日期。

I am using reactive form here and i had already assigned NgbDateStruct Date as the form value我在这里使用的是响应式表单,并且我已经将 NgbDateStruct Date 指定为表单值

          const strDateTime = driver['JoinDate'];
          const arr = strDateTime.split(' ');
          const strDate = arr[0];
          const dateArr = strDate.split('/');

          const ngbDate: NgbDateStruct = {
            year: dateArr[2],
            month: dateArr[1],
            day: dateArr[0],
          }
          this.addDriverForm.patchValue({
            dateJoined: ngbDate
          })

value of dateJoined control should be the next structure: dateJoined控件的值应该是下一个结构:

this.dateJoined = new FormControl({
  year: 2020,
  month: 5,
  day: 16,
});

You need to verify that all the props are numbers, not strings.您需要验证所有的道具都是数字,而不是字符串。

const ngbDate: NgbDateStruct = {
  year: parseInt(dateArr[2], 10),
  month: parseInt(dateArr[1], 10),
  day: parseInt(dateArr[0], 10),
}

u can also use NgbDate class object你也可以使用 NgbDate class object

const defaultDate = new NgbDate(
   sDate.getFullYear(),
   sDate.getMonth() + 1,
   sDate.getDate()
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM