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

问题描述

在IE浏览器中,例如,当我键入带有两位数字的年份的日期时:17/12/17,mat-datepicker将年份转换为19/12/17.

我希望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中修复两位数的年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:46