Various dateInput formats supported by mat-datepicker

I'm facing an issue while configuring two mat-datepickers with different date formats - "MM/YYYY" and "DD/MM/YYYY". I attempted to set the format for MM/YYYY in one module and the format for DD/MM/YYYY in the app module.

Here is my first code snippet:

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

providers: [{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
  {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS}]
}; 

And here is my second code snippet:

export const MY_FORMATS = {
  parse: {
    dateInput: 'DD/MM/YYYY',
  },
  display: {
    dateInput: 'DD/MM/YYYY',
    monthYearLabel: 'DD MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'DD MMMM YYYY',
  },
};
...
providers: [{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS}]
})

However, I noticed that if I use the DD/MM/YYYY format in the settings, all dates are displayed as DD/MM/YYYY. When not used, all dates show as MM/YYYY. How can I configure one datepicker to display as MM/YYYY and another as DD/MM/YYYY?

Answer №1

After following the Datepicker examples in Angular Material Wiki, I discovered a solution by defining the DateAdapter and Date Formats providers within the Component instead of the Module. Surprisingly, this approach resolved the issue for me.

Answer №2

<mat-form-field><input
    type="text"
    [value]="VisitorsCurrentDate"
    class="visit_date"
    (click)="patientVisitPicker.open()"
    style="text-transform: capitalize"
    style="border: none" /><input
    matInput
    [matDatepicker]="patientVisitPicker"
    (dateInput)="setPatientsVisitsDateTiming($event.value)"
    style="display: none"
    class="mat_class" /><mat-datepicker-toggle
    matSuffix
    [for]="patientVisitPicker"
    style="display: none"
  ></mat-datepicker-toggle
  ><mat-datepicker
    [dateClass]="dateClass"
    #patientVisitPicker
    style="display: none"
  ></mat-datepicker
></mat-form-field>

public updatePatientVisitsDate(date:any){
    this.VisitorsCurrentDate = moment(date, "D-M-YYYY").format("DD MMM YYYY");
}

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

Unsubscribing from a nested observable - a step-by-step

In our Angular component, we leverage the ngOnDestroy() method to handle canceling http requests that are still pending when navigating away from a page. To avoid reloading data that has already been fetched, we utilize a custom generic cache helper on cer ...

The utilization of functions from a implemented interface results in the generation of a 'non-function' error

I recently created an interface that includes variables and a function. However, I encountered an issue when trying to utilize the implemented function for a specific class as it resulted in an 'ERROR TypeError: ...getPrice is not a function" Below ...

Mandatory classification eliminates understanding of function type

I'm currently trying to transform an optional argument from one type into a required one in my type definition. However, I am encountering some difficulties and can't seem to figure out what I'm doing wrong in the process. Would appreciate a ...

Obtain redirected JSON data locally using Angular 5

Currently, I am working on retrieving JSON data which will be sent to my localhost through a POST method. The SpringBoot API controller will validate the JSON content before forwarding it to my localhost. My task is to intercept this JSON data when it is t ...

Retrieve the document id along with the corresponding data from a collection

Retrieving data from the collection leads and displaying all documents in an HTML table. Upon clicking, I need to extract the document ID of the clicked item as it serves as the sole primary key. P.S I am able to obtain records of all documents in an arra ...

The function validation_1.validateName in Ionic/Angular is not recognized as a valid function, causing an error that prevents

My development environment includes Ionic Angular. After upgrading from Angular 9 to Angular 14 and Ionic 4 to Ionic 5, I encountered an issue where I can no longer generate pages or components using the command: ionic g page [PATH] The process now trigge ...

Wildcard routes taking precedence over other defined routes

Currently, I'm developing a Node.js server utilizing Express.js and Typescript. Within my project structure, there is a folder named "routes" where I store .ts files containing route definitions. An example of a route file might appear like this: impo ...

What is the best way to organize the data retrieved from the api into a map?

In my search page component, I display the search results based on the user's query input. Here is the code snippet: "use client"; import { useSearchParams } from "next/navigation"; import useFetch from "../hooks/useFetch&qu ...

How can a child component class in Angular 2 be initialized when extending a parent class?

Let's consider a scenario where we have a component called ChildComponent that extends from the Parent class. How do we go about initializing the ChildComponent class? In Angular 2, when this component is used in HTML, it automatically invokes the Chi ...

Utilizing the p tag to incorporate dynamic data in a single line

I am currently working with two JSON files named contacts and workers. Using the workerId present in the contacts JSON, I have implemented a loop to display workers associated with each contact. The current display looks like this: https://i.sstatic.net/AR ...

Components on different routes are not being triggered by the `BehaviourSubject` subscription

In my current project, I am working on transferring data between components using a subject. I have created a service for this purpose, and the two components involved are imagescust and imagesipa. The data is being passed from the imagesipa component and ...

Tips for incorporating a token parameter into an Angular route

Currently, I am working on a project that involves Angular-12 frontend and Laravel-8 backend for user registration confirmation using tokens. For user activation through email in the Angular frontend, I have set up the route from the API backend like this ...

Form in Angular 6 does not trigger the event

I am currently utilizing Angular 6 and have created an attribute directive. Within this directive, I have defined a click event. @Directive({ selector: "[pa-attr]" }) export class PaAttrDirective { @Input("pa-attr") @HostBinding("class") bgC ...

Exploring TypeScript Generics and the Concept of Function Overloading

How can I create a factory function that returns another function and accepts either one or two generic types (R and an optional P) in TypeScript? If only one generic type is provided, the factory function should return a function with the shape () => ...

How to extract a value from a BehaviorSubject within an Angular 6 guard

I have chosen this approach because I have another guard responsible for validating the user based on a token that was previously stored. This is how it was handled in previous versions of rxjs, but now with the latest version you can no longer use map on ...

Routing in nested modules for Angular 7

I am struggling to understand how the routing works with multiple nested modules. The RouteTree seems correct to me, but I am facing an issue after navigating to the "ContactPage" where the URL changes but the view does not render. Below is a snippet of m ...

Angular error: The property 'component' cannot be read because it is null

I encountered an unusual problem with my route configuration. Here is a snippet of the basic routes: const appRoutes: Routes = [ {path: '', redirectTo: '/search', pathMatch: 'full'}, {path: 'login', component: L ...

Sort the list by the last name using Angular 2

Is there a way to arrange the contact list by LAST NAME, with names categorized under each alphabet? While I was able to achieve this in jQuery and Angular-1, I need guidance on how to implement this logic in Angular2/Ionic V2. ...

Converting ISO date format to 'MM/DD/YYYY' format in a Bootstrap/Angular application

Is there a way to convert an ISO date string to 'MM/DD/YYYY' on load in a bootstrap/angular environment? Upon loading, the date picker currently displays like this: https://i.sstatic.net/g5MkI.jpg We want the user to input the date in 'MM/ ...

Angular 2 TypeScript: Accelerating the Increment Number Speed

I'm working with a function in Angular 4 that is triggered when the arrow down key is pressed. Each time the arrow down key is hit, the counter increments by 1. In this function, I need to run another function if the counter reaches a certain speed. ...