Den*_*uka 3 angular ngb-datepicker angular6 angular-template-form
在我的 Angular 应用程序模板驱动表单中,我有一个出生日期字段,并为该字段添加了 ngbDatepicker 作为给定。
<input #dob="ngbDatepicker"
(click)="dob.toggle()"
[(ngModel)]="model.dob"
[ngClass]="{ 'is-invalid': f.submitted && dob.invalid }"
class="form-control"
id="dob"
name="dob"
required
type="text"
[disabled]="isDisabled"
[class.ash]="isDisabled"
[class.highlight]="!isDisabled"
ngbDatepicker
/>
Run Code Online (Sandbox Code Playgroud)
我正在从后端 API 获取出生日期,dob: "2019-02-16 00:00:00"并且我想像下面那样传递该值,
{
"year": 2019,
"month": 2,
"day": 26
}
Run Code Online (Sandbox Code Playgroud)
因为 ngbDatepicker 以该格式获取值。这就是我试图转换我的出生日期。
toDate(dob) {
const [year, month, day] = dob.split('-');
const obj = { year: year, month: month, day: day.split(' ')[0].trim() };
console.log('convert date', obj);
this.model.dob = obj;
// this.model.dob ={year: 2017, month: 5, day: 13};
}
Run Code Online (Sandbox Code Playgroud)
输出是{year: "2019", month: "02", day: "16"},我想从此输出中删除引号。我尝试了很多方法,但无法获得所需的输出。我可以{"year": 2019, "month": 02, "day": 16}使用波纹管代码获得此输出。
JSON.stringify({ "year": "2019", "month": "2", "day": "26" }, (key, value) => !isNaN(+value) ? +value : value);
Run Code Online (Sandbox Code Playgroud)
但是要设置日期,我需要像这样设置对象。 {year: 2019, month: 2, day: 26 }
我通过这样做解决了我的问题。
创建 NgbDate 实例,
date = new NgbDate(2020,19,02);
Run Code Online (Sandbox Code Playgroud)
将此日期设为 ngModel;
| 归档时间: |
|
| 查看次数: |
8559 次 |
| 最近记录: |