我已将日期设置为格式化 yyyy-MM-dd,我需要让我的组件返回这样的日期。在输入字段中,一切正常,例如 2020-10-03,但是当我尝试时,console.log()
我得到了这个:
endDate: Sat Oct 03 2020 00:00:00 GMT+0300 (IDT)
而且我只需要与数据库输入中相同的字符串。
HTML 代码:
<mat-form-field color="accent">
<mat-label>End date</mat-label>
<input matInput
[matDatepicker]="endDate"
[formControl]="form.get('endDate')"
[errorStateMatcher]="matcher"
>
<mat-error *ngIf="form.get('endDate').hasError('required')">
End date is <strong>required</strong>
</mat-error>
<mat-datepicker-toggle matSuffix [for]="endDate"></mat-datepicker-toggle>
<mat-datepicker #endDate color="primary"></mat-datepicker>
</mat-form-field>
Angular 组件代码:
export const PICK_FORMATS = {
parse: {dateInput: {year: 'numeric',month: 'numeric', day: 'numeric'}},
display: {
dateInput: 'input',
dateAllyLabel: {year: 'numeric', month: 'numeric', day: 'numeric'},
}
}
export class PickDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
if (displayFormat === 'input') {
return new DatePipe('en-US').transform(date, 'yyyy-MM-dd')
} else {
return date.toString()
}
}
}
@Component({
selector: 'app-update-coupon',
templateUrl: './update-company-coupon.component.html',
styleUrls: ['../../../app/shared/layouts/company/company.component.css'],
providers: [
{provide: DateAdapter, useClass: PickDateAdapter},
{provide: MAT_DATE_FORMATS, useValue: PICK_FORMATS}
]
})
export class UpdateCompanyCouponComponent {
form = new FormGroup({
id: new FormControl('', [
Validators.required
]),
endDate: new FormControl('', [
Validators.required
]),
price: new FormControl('', [
Validators.min(1),
Validators.required
]),
amount: new FormControl('', [
Validators.min(1),
Validators.required
])
})
onSubmit() {
console.log(this.form.value)
}
我将不胜感激任何帮助!