In my Angular 5 application, I had implemented a custom date adapter as follows:
import {NativeDateAdapter} from "@angular/material";
import {Injectable} from "@angular/core";
@Injectable()
export class CustomDateAdapter extends NativeDateAdapter {
parse(value: any): Date | null {
if ((typeof value === 'string') && (value.indexOf('/') > -1)) {
const str = value.split('/');
return new Date(Number(str[2]), Number(str[1])-1, Number(str[0]), 12);
}
const timestamp = typeof value === 'number' ? value : Date.parse(value);
return isNaN(timestamp) ? null : new Date(timestamp);
}
format(date: Date, displayFormat: Object): string {
return date.getFullYear() + "-" + (date.getMonth() + 1).toString().padStart(2, '0') + "-" + date.getDate().toString().padStart(2, '0');
}
}
This custom date adapter was defined in app.module.ts.
@NgModule({
imports: [...],
declarations: [...],
providers: [
...,
{ provide: DateAdapter, useClass: CustomDateAdapter }
],
bootstrap: [
AppComponent
]
})
export class AppModule {}
However, after upgrading to Angular/Material version 6, the custom date adapter stopped working and an error started occurring. The error message is:
ERROR TypeError: Cannot read property 'TRIDENT' of undefined
at new NativeDateAdapter (native-date-adapter.ts:83)
at new CustomDateAdapter (custom.date.adapter.ts:5)
at _createClass (ng_module.ts:171)
at _createProviderInstance$1 (ng_module.ts:143)
at resolveNgModuleDep (ng_module.ts:104)
at NgModuleRef_.get (refs.ts:502)
at resolveDep (provider.ts:416)
at createClass (provider.ts:276)
at createDirectiveInstance (provider.ts:135)
at createViewNodes (view.ts:303)
I found a solution on Stack Overflow where it was suggested to add a constructor:
constructor(matDateLocale: string) {
super(matDateLocale, new Platform());
}
After adding this constructor, a new error occurred:
ERROR Error: StaticInjectorError(AppModule)[DateAdapter -> String]:
StaticInjectorError(Platform: core)[DateAdapter -> String]:
NullInjectorError: No provider for String!
at NullInjector.get (injector.ts:35)
at resolveToken (injector.ts:332)
at tryResolveToken (injector.ts:274)
at StaticInjector.get (injector.ts:154)
at resolveToken (injector.ts:332)
at tryResolveToken (injector.ts:274)
at StaticInjector.get (injector.ts:154)
at resolveNgModuleDep (ng_module.ts:124)
at _createClass (ng_module.ts:173)
at _createProviderInstance$1 (ng_module.ts:143)
At this point, I am seeking suggestions on how to resolve this issue and make the custom date adapter work again.