Angularmaterial日期选择器的输出覆盖
创始人
2024-10-28 10:01:14
0

可以通过覆盖日期选择器的MatDateFormats提供的方法来覆盖日期或时间的输出格式。以下是一个示例,将日期输出为YYYY年MM月DD日的格式:

  1. 创建一个新的文件my-date-format.ts,其中包含以下内容:

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

@Injectable() export class MyDateAdapter extends NativeDateAdapter {

format(date: Date, displayFormat: Object): string {
    if (displayFormat === 'YYYY-MM-DD') {
        return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日';
    } else {
        return super.format(date, displayFormat);
    }
}

}

export const MY_DATE_FORMATS: MatDateFormats = { parse: { dateInput: 'YYYY-MM-DD', }, display: { dateInput: 'YYYY-MM-DD', monthYearLabel: 'yyyy MMMM', dateA11yLabel: 'yyyy MMMM d', monthYearA11yLabel: 'yyyy MMMM', }, myDateFormat: { monthYearLabel: 'YYYY年MM月', dateA11yLabel: 'YYYY年MM月DD日', monthYearA11yLabel: 'YYYY年MM月', // This will be used by [matDatepicker] as a fallback if myDateFormat.monthYearLabel is not specified // and [matDatepickerRange] will take this as range separator format. rangeSeparator: ' - ' } };

  1. 在app.module.ts文件中将MyDateAdapter和MY_DATE_FORMATS导入,如下所示:

import { MatNativeDateModule, DateAdapter, MAT_DATE_FORMATS } from '@angular/material'; import { MyDateAdapter, MY_DATE_FORMATS } from './my-date-format';

@NgModule({ imports: [ //... MatNativeDateModule, //... ], providers: [ //... { provide: DateAdapter, useClass: MyDateAdapter }, { provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS } //... ] })

相关内容

热门资讯

透视模拟器!aapoker辅助... 透视模拟器!aapoker辅助器怎么用(透视)辅助(确实是有挂)1、aapoker辅助器怎么用透视辅...
透视实锤!德普之星透视软件免费... 透视实锤!德普之星透视软件免费入口官网(透视)免费透视(竟然真的有挂)1、德普之星透视软件免费入口官...
透视免费!aapoker真的假... 透视免费!aapoker真的假的(透视)插件(果然是真的有挂)进入游戏-大厅左侧-新手福利-激活码辅...
透视黑科技!德普之星透视(透视... 透视黑科技!德普之星透视(透视)透视辅助软件(果然是真的有挂)1、全新机制【德普之星透视软件透明挂】...
透视黑科技!aapoker透视... 透视黑科技!aapoker透视方法(透视)辅助(好像是真的有挂)1、该软件可以轻松地帮助玩家将aap...
透视线上!德普之星辅助软件(透... 透视线上!德普之星辅助软件(透视)透视辅助插件(真是有挂)1、进入游戏-大厅左侧-新手福利-激活码辅...
透视美元局!aapoker破解... 透视美元局!aapoker破解侠是真的吗(透视)插件(一贯真的有挂);一、aapoker破解侠是真的...
透视规律!德扑圈透视(透视)透... 透视规律!德扑圈透视(透视)透视(好像是有挂)1、完成德扑圈透视透视辅助安装,帮助玩家取得所有比赛的...
透视攻略!aapoker ai... 透视攻略!aapoker ai插件(透视)插件(好像有挂)1、操作简单,无需注册,只需要使用手机进行...
透视脚本!德扑圈有透视吗(透视... 透视脚本!德扑圈有透视吗(透视)透视辅助软件(一直是有挂)1、上手简单,内置详细流程视频教学,新手小...