The Angular Material date picker's keyboard input format is functional only in Chrome

I'm facing a challenge with the date picker component from Angular Material. When I try to manually type in a date like "2019.12.20" instead of selecting it, the input only works in Google Chrome. But when I tested it on Edge and Firefox, the date value appears as null with this format. Interestingly, if I use "-" or "/" instead of ".", for example, "2019-12-20," it works smoothly without any issues.

My providers are configured as follows:

providers: [
  { provide: MAT_DATE_LOCALE, useValue: 'hu' },
  { provide: DateAdapter, useClass: AppDateAdapter },
  { provide: MAT_DATE_FORMATS, useValue: APP_DATE_FORMATS }            
]

This is my implementation of DateAdapter:

import { NativeDateAdapter } from '@angular/material';


export class AppDateAdapter extends NativeDateAdapter {

  format(date: Date, displayFormat: Object): string {

    if (displayFormat === 'input') {

      const day = date.getDate();
      const month = date.getMonth() + 1;
      const year = date.getFullYear();

      return `${year}.${month}.${day}`;
    }

    return date.toDateString();
  }
}

export const APP_DATE_FORMATS =
  {
    parse: {
      dateInput: { month: 'short', year: 'numeric', day: 'numeric' },
    },
    display: {
      dateInput: 'input',
      monthYearLabel: { year: 'numeric', month: 'numeric' },
      dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
      monthYearA11yLabel: { year: 'numeric', month: 'long' },
    }
  };

If you have any insights on what might be causing this issue, or if you need more details regarding the problem, please let me know.

Answer №1

It seems like the issue lies with NativeDateAdapter in Edge and IE. Consider using MomentDateAdapter instead.

Here's a quick explanation: Use MomentDateAdapter in the provider, as shown in the code snippet below:

TS file

import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

import * as _moment from 'moment';

const moment = _moment;

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

/** @title Datepicker with custom formats */
@Component({
  selector: 'datepicker-formats-example',
  templateUrl: 'datepicker-formats-example.html',
  styleUrls: ['datepicker-formats-example.css'],
  providers: [
     { provide: MAT_DATE_LOCALE, useValue: 'hu' },
    {
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },
    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})
export class DatepickerFormatsExample {
  date = new FormControl(moment());
}

HTML file

<mat-form-field>
  <input matInput [matDatepicker]="dp" placeholder="Verbose datepicker" [formControl]="date">
  <mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
  <mat-datepicker #dp></mat-datepicker>
</mat-form-field>

In the example above, the date field accepts input formats such as

['YYYY.MM.DD', 'YYYY-MM-DD', 'YYYY/MM/DD', 'YYYY MM DD']
, and displays them in the format YYYY MMM DD.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for compressing HTML templates using angular2 and webpack?

I have implemented the webpack starter found at this link: https://github.com/AngularClass/angular2-webpack-starter Within the webpack config, I came across the following code snippet (I included minify options myself) new HtmlWebpackPlugin({ template: ...

Failed to access element from the array using specified id

Seeking assistance with creating dynamic pages for each object in the boxArray file. I've developed a service called boxService to extract objects. While I am able to retrieve all elements successfully, encountering errors when attempting to extract i ...

Guide on invoking an HTML event method from a parent component to a child component

I have the following parent component: JS import { Component, OnInit } from '@angular/core'; import { TaskService } from '../task.service'; import { ViewChild } from '@angular/core/src/metadata/di'; import { CreateNewTaskCom ...

Tips for sidestepping CORS issues when using sendFile in Express.js

After successfully deploying my application on Heroku and setting up file serving using Express Js, I encountered a CORS issue when sending requests from the frontend (Angular). This problem only occurs with the route for sending files. Below is the Expre ...

Ways to incorporate Bootstrap components into an Angular application without relying on external modules

I'm attempting to activate Bootstrap 5.3 Tooltips within my Angular 17 application. Within angular.json, I've added bootstrap.bundle.min.js, which includes PopperJS. "scripts": [ "node_modules/bootstrap/dist/js/bootstrap.bundle. ...

Leverage ngFor to loop through a "highly intricate" data structure

In my project, I have stored data in a JSON file structured as follows: { "name": { "source1": ____, "source2": ____, "source3": ____ }, "xcoord": { "source1": ____, "source2": ____, "source3": _ ...

Guide to executing specific functions based on selected dropdown options in Angular 5

Several drop down menus need to be implemented. The requirement is that when an option from the drop-down is selected, a corresponding custom function should be called. However, in the code below, what should replace opt.mtd since it is not functioning as ...

Having issues with autocompletion in the input element of Angular 7 Material Design

My Angular 7 application includes a Material Design form with input text fields, and I have not implemented any autocomplete feature within the code. Despite deleting all navigation data from my Chrome browser, I am still experiencing autocomplete suggesti ...

Fake AxiosInstance. In need of obtaining response in a single test

In my api.ts file import axios from 'axios' export const api = axios.create({ baseURL: 'http://localhost:3333/', }) Within my react-page.spec.tsx file: import React from 'react' import '@testing-library/jest-dom&apo ...

Combining Django Rest Framework API with Angular 2 for seamless integration

Currently, I am working on building a back-end API using Django Rest Framework and a front-end using Angular 2. The Django server is running on localhost:8000 and the Angular server on localhost:3000. However, I am encountering an error when trying to acce ...

Setting a property with a generic type array: Tips and tricks

Currently, I am working on implementing a select component that can accept an array of any type. However, I am facing some challenges in defining the generic and where to specify it. My project involves using <script setup> with TypeScript. Here is ...

Tips for implementing the handleChange event with CalendarComponent from the PrimeReact library

Hey there! I'm currently working with the CalendarComponent from the PrimeReact library in my app. I want to update the type of event being typed in the handleChange function instead of leaving it as :any. Can anyone provide some suggestions on what s ...

What is the best way to activate a component within Angular 2 that triggers the display of another component through method invocation?

I have created a popup component that can be shown and hidden by calling specific methods that manipulate the back and front variables associated with the class. hide() { this.back = this.back.replace(/[ ]?shown/, ""); this.front = this.front.replace( ...

When Typescript calls the toString method on a Function, it produces unexpected characters like "path_1, (0, promises.writeFile)"

I'm currently attempting to convert a function into a string for transmission to a worker thread for execution. However, when imported code is included, the resulting string contains strange characters. import { HttpStatus } from '@nestjs/common& ...

Leveraging the keyof keyword to access a specific property within a type

I have a variety of types that I need to work with. For example: type Type = { prop1: number; prop2: string; prop3: someOtherType } type Props = keyof Type I am aware that using an "indexed access type" allows me to extract the type of propN, ...

What are the necessary steps to deploy Angular 2 universal starter on a third-party server host such as Google Cloud or Azure?

I recently cloned the universal-starter (webpack version) repository and successfully got it running on my local machine using npm start and npm run watch as per the provided instructions. However, I hit a roadblock after running npm run build and trying ...

What causes the typings for Partial Record to be undefined when using Object.values?

When I retrieve Object.values from a Partial Record, the values consist of a combination of what I anticipated and undefined. const example: Partial<Record<string, number>> = {} const values = Object.values(example) // The type for values is u ...

Angular is encountering an issue where it is unable to access a property of undefined on an

There appears to be a puzzling issue in this code snippet. Despite my best efforts, it keeps throwing an error that I can't seem to pinpoint. Perhaps someone with a fresh perspective can spot the problem... The class is lengthy, but I felt it was nec ...

Binding two objects to a single event using Angular 2 syntax

Is there a way to connect two simple input fields to a single click event in Angular? One box for typing text and the other providing a timestamp from Date();. How can I show both values when clicking on the button? // The #date input field provides the ...

Tips for expanding an md-nav-item in Angular Material

Need help on stretching the md-nav-item element illustration 1 ------------------------------------------------ | ITEM1 | ITEM 2 | ------------------------------------------------ illustration 2 ------------------------------------------------ | ...