【问题标题】:Mat date picker custom value in Angular getting errorAngular 中的 Mat 日期选择器自定义值出现错误
【发布时间】:2020-03-08 08:58:32
【问题描述】:

我正在研究 Mat 日期选择器自定义值。这是我正在尝试的代码

      <div class="form-group" *ngIf="selectVal.value == 'Date'">
                <mat-form-field>
                <input matInput [matDatepicker]="picker" (dateChange)="formatDate(dateValue.value)" #dateValue  formControlName="assumptionValueInDatetime" class="form-control" placeholder="Choose a date" >
                    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                    <mat-datepicker #picker></mat-datepicker>
                </mat-form-field>
            </div>

这是我的 ts 代码。这是我想要的格式 07-JUL-2020

    formatDate(value) {
        const months = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
        let formatDate = ('0' + value.getDate()).slice(-2) + "-" + months[value.getMonth()] + "-" + (value.getFullYear());
        return formatDate;
    }

这是我的有效载荷的样子:

    initassumpationForm() {
        this.assumptionDesignForm = this.fb.group({
            assumptionValueInDatetime: this.formatDate(this.assumptionValueInDatetime)
        });     
    }

得到错误为:

错误类型错误:无法读取未定义的属性“getDate”

【问题讨论】:

  • 试试这个:新日期(this.assumptionValueInDatetime)
  • @Chellappanவ 如果我使用这种格式,它将如何转换为 2020 年 6 月 12 日任何想法
  • @Chellappanவ 它抛出错误

标签: angular angular8 mat mat-datepicker


【解决方案1】:

最好的方法是构建一个自定义日期适配器(它是一项服务)。当您使用 JavaScript Date 对象作为基础日期格式时,您只需扩展 @angular/material/core 中提供的 NativeDateAdapter 并覆盖两个方法:parse 和 format。前者从您的字符串格式中获取Date 对象(例如:01-MAR-2020),后者从Date 对象中获取一个字符串对象(如问题中提供的formatDate 函数)文本,确实)。

构建您的自定义本机日期适配器类后,只需使用标记 DateAdapter 在您的 AppModule 中提供它:

import {DateAdapter} from '@angular/material/core';
...

@NgModule({
  ...
  providers: [{provide: DateAdapter, useClass: CustomNativeDateAdapter}]
  ...
})
export class AppModule {}

关于 CustomNativeDateAdapter 类,在您的情况下,它将类似于:

import { Injectable } from '@angular/core';
import { NativeDateAdapter } from '@angular/material/core';

@Injectable()
export class CustomNativeDateAdapter extends NativeDateAdapter {
  months: string[] = [ 'JAN', 'FEB', 'MAR', 'APR',  'MAY', 
    'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' ];

  monthsObj: { [key: string]: number } = { JAN: 0, FEB: 1, MAR: 2, APR: 3, 
    MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11 };

  parse(value: any): Date | null {
    if (typeof value == 'number') {
      return new Date(value);
    }

    const dateRegex: RegExp = /^(0|1)[1-9]-[A-Z]{3}-(1|2)[0-9]{3}$/;
    if (typeof value == 'string' && dateRegex.test(value)) {
      const dateSplit = value.split('-');
      return new Date(+dateSplit[2], this.monthsObj[dateSplit[1]],
        +dateSplit[0], 12, 0, 0, 0);
    }

    return null;
  }

  format(date: Date, displayFormat: Object): string {
    if (!this.isValid(date)) {
      throw Error('NativeDateAdapter: Cannot format invalid date.');
    }

    let formatDate = ('0' + date.getDate()).slice(-2) +
      '-' + this.months[date.getMonth()] + '-' + date.getFullYear();
    return formatDate;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多