如何在 IE 浏览器的 mat-datepicker 中修复两位数的年份? [英] How do I fix a year of two digits in mat-datepicker in IE browser?

查看:23
本文介绍了如何在 IE 浏览器的 mat-datepicker 中修复两位数的年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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-datepicker:

In IE browser When I type a date with a year of two digits for example: 03/12/17 the mat-datepicker converts the year to 03/12/1917.

I want mat-datepicker to convert the year 17 to the current year 2017 for example if I type 03/12/17 the mat-datepicker should convert that date to 03/12/2017.

In Chrome browser the mat-datepicker converts the two digits year correctly 17 to 2017.

mat-datepicker: https://material.angular.io/components/datepicker/overview

Is there any solution for that problem in IE browser?

Thanks.

解决方案

Try to use the following code:

Code in html page:

<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>

Code in the .ts file:

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());
}

Then, the result like this:

这篇关于如何在 IE 浏览器的 mat-datepicker 中修复两位数的年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆