首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在IE浏览器的mat-datepicker中修复两位数的年份?

如何在IE浏览器的mat-datepicker中修复两位数的年份?
EN

Stack Overflow用户
提问于 2019-03-06 22:41:36
回答 1查看 1.3K关注 0票数 0

在IE浏览器中,当我键入包含两位数字的日期时,例如: 03/12/17,mat-datepicker会将年份转换为03/12/1917。

我希望mat-datepicker将年份17转换为当前年份2017,例如,如果我输入03/12/17,则mat-datepicker应该将该日期转换为03/12/2017。

在Chrome浏览器中,mat-datepicker将两位数的年份正确地转换为17到2017年。

mat-日期选择器:https://material.angular.io/components/datepicker/overview

在IE浏览器中有解决这个问题的方法吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 10:49:30

尝试使用以下代码:

html页面中的代码:

代码语言:javascript
复制
<mat-form-field>
  <input matInput [matDatepicker]="dp" placeholder="Month and Year" [formControl]="date">
  <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
  <mat-datepicker #dp
                  startView="multi-year"
                  (yearSelected)="chosenYearHandler($event)"
                  (monthSelected)="chosenMonthHandler($event, dp)"
                  panelClass="example-month-picker">
  </mat-datepicker>
</mat-form-field>

.ts文件中的代码:

代码语言:javascript
复制
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as _moment from 'moment';

const moment =  _moment;

export const MY_FORMATS = {
  parse: {
    dateInput: 'MM/DD/YYYY',
  },
  display: {
    dateInput: 'MM/DD/YYYY',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};

@Component({
  selector: 'app-material-datepicker',
  templateUrl: 'material-datepicker.component.html',
  styleUrls: ['material-datepicker.component.css'],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},

    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})
export class MaterialDatepickerComponent {

  constructor(private adapter: DateAdapter<Date>) {
  }
  date = new FormControl(moment());
}

然后,结果如下:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55025706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档