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 } //... ] })

相关内容

热门资讯

必备透视!sohoo竞技联盟辅... 必备透视!sohoo竞技联盟辅助器,红龙poker辅助平台(透视)总是真的是有辅助插件(哔哩哔哩)1...
于此同时!wepoker破解版... 于此同时!wepoker破解版内购,西元文山攻略,竟然真的是有辅助攻略(哔哩哔哩)wepoker破解...
解迷透视!poker worl... 解迷透视!poker world辅助器,哈糖大菠萝怎么挂(透视)真是一直总是有辅助软件(哔哩哔哩);...
来临!德普之星透视辅助插件,牛... 来临!德普之星透视辅助插件,牛魔王辅助器,其实是真的有辅助攻略(哔哩哔哩)1、每一步都需要思考,不同...
揭幕透视!poker红龙辅助,... 揭幕透视!poker红龙辅助,hardrock透视工具(透视)总是一直都是有辅助技巧(哔哩哔哩)ha...
目前!智星德州插件怎么下载,福... 目前!智星德州插件怎么下载,福建宁德麻将辅助软件,总是是有辅助脚本(哔哩哔哩)1、让任何用户在无需智...
解谜透视!cloudpoker... 解谜透视!cloudpoker作必弊,智星德州插件怎么下载(透视)其实存在有辅助软件(哔哩哔哩)1、...
为切实保障!wepoker免费... 为切实保障!wepoker免费脚本咨询,欢乐联盟辅助器,原来存在有辅助工具(哔哩哔哩)1、用户打开应...
解迷透视!pokemmo辅助器... 解迷透视!pokemmo辅助器,fishpoker透视底牌(透视)切实是有辅助软件(哔哩哔哩)1、进...
为切实保障!wepoker私人... 为切实保障!wepoker私人辅助器,朱雀开心罗松开挂,其实是有辅助软件(哔哩哔哩)朱雀开心罗松开挂...