嗨,我正在使用ngbDatePicker,我的格式是YYYY-MM-DD,我试图更改它,但似乎无法将格式更改为MM/DD/YYYY。
这是我的html代码
<div class="form-group datepicker">
<label for="dob">Date of Birth*</label>
<div class="row input-group">
<input
ngbDatepicker
#d="ngbDatepicker"
#dobF="ngModel"
class="form-control input-underline input-lg"
id="dob"
[(ngModel)]="dateOfBirth"
placeholder="mm-dd-yyyy"
name="dp"
[ngClass]="{
invalid:
(dobF.value === null || isString(dobF.value) || futureDate) && dobF.touched
}"
required
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="d.toggle()"
type="button"
></button>
</div>
</div>
<div
*ngIf="
(dobF.value === null || isString(dobF.value) || futureDate) && dobF.touched
"
class="error"
>
Please enter a valid date of birth.
</div>
</div>
这是我的ts文件代码
public dateOfBirth: { year: number; month: number; day: number };
public currentDate = moment().format("YYYY-MM-DD");
constructor(
private config: NgbDatepickerConfig
) {
// customize default values of datepickers used by this component tree
const currentDate = new Date();
const day = currentDate.getDate();
const month = currentDate.getMonth() + 1;
const year = currentDate.getFullYear();
this.config.minDate = {year: 1900, month: 1, day: 1};
this.config.maxDate = {year, month, day};
this.config.outsideDays = "hidden";
}
ngOninit() {
this.subscriptions["patient"] = this.store
.select("patient")
.subscribe(data => {
this.patient = Object.assign({}, this.patient, data);
const dob = this.patient.Dob ? this.patient.Dob.split("-") : null;
dob !== null
? (this.dateOfBirth = {
year: Number(dob[0]),
month: Number(dob[1]),
day: Number(dob[2])
})
: (this.dateOfBirth = null);
});
}
ngAfterContentChecked() {
let currentDateObject = this.currentDate.split("-");
this.dobYear = Number(currentDateObject[0]);
this.dobMonth = Number(currentDateObject[1]);
this.dobDay = Number(currentDateObject[2]);
if (this.dateOfBirth) {
this.futureDate = this.dateOfBirth.year > this.dobYear || (this.dateOfBirth.year == this.dobYear && this.dateOfBirth.month > this.dobMonth)
|| (this.dateOfBirth.year == this.dobYear && this.dateOfBirth.month == this.dobMonth && this.dateOfBirth.day > this.dobDay);
}
}
我似乎找不到任何帮助来改变格式.我怎样才能改变格式从YYYY/DD/MM到MM/DD/YYYY.有任何帮助吗谢谢
2条答案
按热度按时间svmlkihl1#
您需要扩展NgbDateParserFormatter并覆盖默认提供程序:
下面是dd/mm/yyyy的一个例子,您只需要修改
parse()
和format()
函数,将其更改为mm/dd/yyyy。添加以下类:
修改app.module.ts以覆盖默认提供程序:
kjthegm62#
最好的解决方案是修改下面的文件,我已经编辑了代码,以获得所需的格式(日-月-年)在我的输入字段。
ngb-date-parser-formatter.js
从下面的代码中,您将在输入字段中获得DD-MM-YYYY****日期格式。