How do I fix a year of two digits in mat-datepicker in IE browser?

橙三吉。 提交于 2021-01-29 06:32:11

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/55025706/how-do-i-fix-a-year-of-two-digits-in-mat-datepicker-in-ie-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!