Ashutosh Soni
Ashutosh Soni

Reputation: 1025

MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule

I have imported the matdatepicker in my app.module.ts file but still it shows some error. I can't get the material component working. the button works fine and uses angular material component. But when I use datepicker it doesn't work.

app.module.ts file

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import{ HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatTableModule} from '@angular/material/table';
import {MatDatepickerModule} from '@angular/material/datepicker';
import {MatButtonModule} from '@angular/material/button';
import {MatFormFieldModule} from '@angular/material/form-field';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { InactiveusersComponent } from './component/inactiveusers/inactiveusers.component';
import { CalendarComponent } from './component/calendar/calendar.component';
import { InactiveItemComponent } from './component/inactive-item/inactive-item.component';
import { NewtableComponent } from './component/newtable/newtable.component';

@NgModule({
  declarations: [
    AppComponent,
    InactiveusersComponent,
    CalendarComponent,
    InactiveItemComponent,
    NewtableComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    BrowserAnimationsModule,
    MatTableModule,
    MatDatepickerModule,
    MatButtonModule,
    MatFormFieldModule
  ],
  exports:[
    MatDatepickerModule,
    MatButtonModule
  ],
  providers: [MatFormFieldModule,MatDatepickerModule],
  bootstrap: [AppComponent]
})
export class AppModule { }

calendar.component.html

<input #startDate  type="date" name="startdate">
<input #endDate type="date"name="enddate">

<button (click)="sendRange(startDate.value,endDate.value)" mat-button color="primary">Submit</button>

<mat-form-field>
    <input matInput [matDatepicker]="picker" placeholder="Choose a date">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>

ERROR Error: MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule, MatMomentDateModule, or provide a custom implementation.
    at createMissingDateImplError (datepicker.js:37)
    at new MatDatepickerInput (datepicker.js:2344)
    at createClass (core.js:24577)
    at createDirectiveInstance (core.js:24386)
    at createViewNodes (core.js:34994)
    at callViewAction (core.js:35444)
    at execComponentViewsAction (core.js:35349)
    at createViewNodes (core.js:35023)
    at callViewAction (core.js:35444)
    at execComponentViewsAction (core.js:35349)

Upvotes: 39

Views: 101186

Answers (9)

Mahesh vishwakarma
Mahesh vishwakarma

Reputation: 11

import { MatNativeDateModule } from '@angular/material/core';

imports: [ MatNativeDateModule

]

Upvotes: 1

Dmitry Avdoshin
Dmitry Avdoshin

Reputation: 91

Angular 17

I solved the problem by adding the provider importProvidersFrom(MatNativeDateModule) to app.config.ts

Full code:

import {ApplicationConfig, importProvidersFrom} from '@angular/core';
import {provideRouter} from '@angular/router';

import {routes} from './app.routes';
import {provideAnimations} from '@angular/platform-browser/animations';
import {provideHttpClient} from "@angular/common/http";
import {MatNativeDateModule} from "@angular/material/core";

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(routes),
    provideAnimations(),
    provideHttpClient(),
    importProvidersFrom(MatNativeDateModule)//here
  ],
};

Upvotes: 6

Wah&#233;b
Wah&#233;b

Reputation: 781

I got the same issue and it was resolved by only adding the following import in my app.module.ts:

import { MatNativeDateModule } from '@angular/material/core';

Upvotes: 1

Mojahid Khan
Mojahid Khan

Reputation: 23

import { MatDatepickerModule } from '@angular/material/datepicker';
 
import {MatNativeDateModule} from '@angular/material/core';
    

This is working fine work me

Upvotes: 2

Achintya Veer Singh
Achintya Veer Singh

Reputation: 71

The imports have changed. Use this instead:

import {MatNativeDateModule} from '@angular/material/core';

Upvotes: 7

DEVolkan
DEVolkan

Reputation: 592

I've solved it by adding follow modules:

import {MatNativeDateModule} from '@angular/material';
import { MatMomentDateModule } from "@angular/material-moment-adapter";

and in your imports

imports: [
    ...
    MatDatepickerModule,
    MatButtonModule,
    MatFormFieldModule,
    MatNativeDateModule, MatMomentDateModule,
  ],

I'm not that deep in material-angular I've only follow the error message advice

ERROR Error: MatDatepicker: No provider found for DateAdapter. You must import one of the following modules at your application root: MatNativeDateModule, MatMomentDateModule, or provide a custom implementation.

Attention: For newer Angular versions you only need to import one module:

import { MatNativeDateModule } from '@angular/material/core';

...
    imports: [
        ...
        MatDatepickerModule,
        MatButtonModule,
        MatFormFieldModule,
        MatNativeDateModule,
      ],

instead:

import {MatNativeDateModule} from '@angular/material';
import { MatMomentDateModule } from "@angular/material-moment-adapter";
...
imports: [
    ...
    MatDatepickerModule,
    MatButtonModule,
    MatFormFieldModule,
    MatNativeDateModule, MatMomentDateModule,
  ],

Upvotes: 51

Madhusanka Edirimanna
Madhusanka Edirimanna

Reputation: 1428

I got, Here working fine Just import the modules in APP.MODULE.TS file

import {MatNativeDateModule} from '@angular/material/core';
imports: [

...

MatNativeDateModule 
],

Upvotes: 65

mnojind
mnojind

Reputation: 101

I got, Here working fine Just import the modules in APP.MODULE.TS file

import {MatDatepickerModule} from '@angular/material/datepicker';
import {MatNativeDateModule} from '@angular/material/core';
import {MatInputModule} from '@angular/material/input';

imports: [
MatDatepickerModule
,MatNativeDateModule,MatInputModule
]

HTML :
You need to wrap element with

Tag
<form>
<mat-form-field>
    <mat-label>Choose a date</mat-label>
    <input matInput [matDatepicker]="picker">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>

Upvotes: 10

I just used the first import referred:

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

the other import got me to another error I didn't solved, but it wasn't necessary.

Don't forget to add MatNativeDateModule to the imports array in app.module.ts.

Upvotes: 6

Related Questions