Dai*_*imz 13 datepicker ng-bootstrap angular
我正在使用ng-bootstrap datepicker,但是当我保存表单时,日期会被保存为.
date: {
day: 01,
month: 12,
year: 16
}
Run Code Online (Sandbox Code Playgroud)
我希望我能把它当成更像的东西 "2016-11-23T00:39:31.768Z"
这是我的实现:
<div class="input-group">
<button class="input-group-btn" (click)="d.toggle()" >
<md-icon svgSrc="assets/images/calendar-plus.svg" style="cursor: pointer;"></md-icon>
</button>
<input class="form-control" placeholder="dd-mm-yyyy" name="dp" formControlName="due_date" navigation="arrows" ngbDatepicker #d="ngbDatepicker">
</div>
Run Code Online (Sandbox Code Playgroud)
和form.component:
constructor( private fb: FormBuilder ) {
this.form = this.fb.group({
due_date: [''],
});
}
Run Code Online (Sandbox Code Playgroud)
sil*_*sod 20
你正在使用ng-bootstrap而不是ng2-bootstrap(不同的组).它背后的代码使用的NgbDateStruct是一个对象{ day, month, year }
在提交时,您需要挂钩并将值更改为其他内容,例如:
onSubmit(): {
let ngbDate = this.form.controls['due_date'].value;
let myDate = new Date(ngbDate.year, ngbDate.month-1, ngbDate.day);
let formValues = this.form.value;
formValues['due_date'] = myDate;
<...>
http.post(url, formValues);
}
Run Code Online (Sandbox Code Playgroud)
https://ng-bootstrap.github.io/#/components/datepicker
NgbDateStruct NgbDatepicker和NgbInputDatepicker指令模型的接口
属性
day类型:number从1开始的月中的某一天
month类型:number月份,默认日历我们使用ISO 8601:1 = Jan ... 12 = Dec
年份类型:数字年份,例如2016年
Tia*_*anM 15
正如@silentsod所提到的,您需要将datepicker使用的日期对象从NgbDateStruct格式转换为string格式.ng-bootstrap提供了将NgbDateStruct格式中的日期转换为ISO 8601格式(yyyy-mm-dd)的功能.如果使用该格式,则无需编写自己的文件:
import {NgbDateParserFormatter} from '@ng-bootstrap/ng-bootstrap';
...
constructor(private ngbDateParserFormatter: NgbDateParserFormatter; ... ) {
...
}
...
onSubmit(): {
let ngbDate = this.form.controls['due_date'].value;
let myDate = this.ngbDateParserFormatter.format(ngbDate); // e.g. myDate = 2017-01-01
...
}
Run Code Online (Sandbox Code Playgroud)
请参阅:https://github.com/ng-bootstrap/ng-bootstrap/blob/master/src/datepicker/ngb-date-parser-formatter.ts
| 归档时间: |
|
| 查看次数: |
20087 次 |
| 最近记录: |