Nel*_*eno 47 momentjs ng-bootstrap angular
我在弹出窗口中使用了datepicker(ng-boostrap),我想将日期格式更改为dd-mm-yyyy.
似乎可以通过实现new NgbDateParserFormatter来替换默认值来解决它NgbDateISOParserFormatter.
但我想知道是否还有另一种方式.
更新:
NgbDateParserFormatter使用Moment.js的一个小实现(使用版本1.0.0-alpha.14的ng-bootstrap测试):
import {NgbDateParserFormatter, NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';
import * as moment from 'moment';
export class NgbDateMomentParserFormatter extends NgbDateParserFormatter {
constructor(private momentFormat: string) {
super();
};
format(date: NgbDateStruct): string {
if (date === null) {
return '';
}
let d = moment({ year: date.year,
month: date.month - 1,
date: date.day });
return d.isValid() ? d.format(this.momentFormat) : '';
}
parse(value: string): NgbDateStruct {
if (!value) {
return null;
}
let d = moment(value, this.momentFormat);
return d.isValid() ? { year: d.year(),
month: d.month() + 1,
day: d.date() } : null;
}
}
Run Code Online (Sandbox Code Playgroud)
在模块中,您使用工厂包含提供程序以指示日期格式作为参数:
---
@NgModule({
---
providers: [
{
provide: NgbDateParserFormatter,
useFactory: () => { return new NgbDateMomentParserFormatter("DD-MM-YYYY") }
}
]
---
})
Run Code Online (Sandbox Code Playgroud)
pko*_*rce 19
截至今天,实施定制NgbDateParserFormatter是最好的方法.是的,这是一种正确的方法.
在未来,我们可能会有一个更复杂的实现NgbDateParserFormatter,您将能够传递所需的格式(例如yyyy-MM-dd).添加此功能取决于用户的兴趣.
您还可以在https://github.com/ng-bootstrap/ng-bootstrap/issues/754#issuecomment-247767027查看更多背景信息.
| 归档时间: |
|
| 查看次数: |
21005 次 |
| 最近记录: |