本文介绍了Angular Material自定义日期选择器格式引发错误"format.replace is not function".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将日期自定义为dd-mmm-yyy,例如对于2019年2月12日,使用角形材料6进行工作,这是我的代码,我没有使用过replace函数,但仍然遇到此错误

I am working on customising the date to the dd-mmm-yyy ie for eg 12-Feb-2019, working using the angular material 6 and this is my code, I have not used a replace function but yet get this error

组件文件内容

 import { MomentDateAdapter ,MatMomentDateModule } from '@angular/material-moment-adapter';
    import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
    import {MatDatepicker} from '@angular/material/datepicker';
    import {default as _rollupMoment, Moment} from 'moment';
    import * as _moment from 'moment';
export class MyDateAdapter extends NativeDateAdapter {
    // constructor() {
    //  super('en-US');
    // }

    format(date: Date, displayFormat: Object): string {
        if (displayFormat == "input") {
            let day = date.getDate();
            let month = date.getMonth() + 1;
            let year = date.getFullYear();
            return this._to2digit(day) + '/' + this._to2digit(month) + '/' + year;
        } else {
            return date.toDateString();
        }
    }

    private _to2digit(n: number) {
        return ('00' + n).slice(-2);
    } 


 }
    export const MY_FORMATS = {
        parse: {
          dateInput: 'LL',
        },
        display: {
            // dateInput: { month: 'short', year: 'numeric', day: 'numeric' },
            dateInput: 'input',
            monthYearLabel: {year: 'numeric', month: 'short'},
            dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
            monthYearA11yLabel: {year: 'numeric', month: 'long'},
        }
      };

还有HTML部分

<div class="col-lg-4 m-form__group-sub">
                                            <mat-form-field class="example-full-width" appearance="outline">
                                                <mat-label>Invoice Date</mat-label>
                                                <input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="picker1" placeholder="Choose a date" formControlName="inv_date">
                                                <mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
                                                <mat-datepicker #picker1></mat-datepicker>
                                            </mat-form-field>
                                        </div>

推荐答案

要具有UTC物料datepicker行为和特定的日期格式:

To have the UTC material datepicker behavior and specific date format:

  1. 安装 @ angular/material-moment-adapter ,并确保还安装了 moment lib.
  2. 导入 MatMomentDateModule .
  3. {提供:MAT_MOMENT_DATE_ADAPTER_OPTIONS,useValue:{useUtc:true}} 添加到模块的提供程序数组.
  4. 可选.要覆盖日期选择器中的显示值,请使用以下语言设置区域设置: {提供:MAT_DATE_LOCALE,useValue:'en-GB'} 以可视化方式为材料日期选择器全局设置dd/M/yyyy格式
  1. Install @angular/material-moment-adapter and make sure moment lib also installed.
  2. Import MatMomentDateModule.
  3. Add { provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true }} to providers array of your module.
  4. Optional. To override the display value in a datepicker, set a locale using: { provide: MAT_DATE_LOCALE, useValue: 'en-GB' } to have globally dd/M/yyyy format visually for the material datepicker

从现在开始,重要日期选择器将与UTC一起使用.datepicker的输出也将是UTC,无需调整时区.

Starting from now, the material datepicker will work with UTC. Output of datepicker will be also UTC without adjustments for a time-zone.

对于非常高级的情况,请使用自定义的DateAdapter实现和DateFormats,而不是上面的所有内容. {provide:DateAdapter,useClass:MyDateAdapter}, {提供:MAT_DATE_FORMATS,useValue:MY_DATE_FORMATS}

For very advanced cases, go with a custom DateAdapter implementation and DateFormats instead of everything above. {provide: DateAdapter, useClass: MyDateAdapter},{provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS}

有关其他信息,请参见重要的API文档

For additional info, look at material API docs

这篇关于Angular Material自定义日期选择器格式引发错误"format.replace is not function".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:33