Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datepicker MAT_DATE_FORMATS #43

Open
All4Gis opened this issue Nov 3, 2021 · 6 comments
Open

Datepicker MAT_DATE_FORMATS #43

All4Gis opened this issue Nov 3, 2021 · 6 comments
Labels
question / support Further information is requested

Comments

@All4Gis
Copy link

All4Gis commented Nov 3, 2021

Hi

I am trying to apply a different format to the datetime (MAT_DATE_FORMATS) but I can't get it to work.

For example, if we use the Material example but use the imports from your library, the date format doesn't work.
https://stackblitz.com/angular/nkdkbolbgvg?file=src%2Fapp%2Fdatepicker-formats-example.ts

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

Can you add an example?
thanks

@matheo
Copy link
Owner

matheo commented Nov 30, 2021

I play with them in the website of this repository,
gotta be careful of all the imports to material/datepicker that could take precedence

@matheo matheo added the question / support Further information is requested label Nov 30, 2021
@All4Gis
Copy link
Author

All4Gis commented Dec 1, 2021

Thanks for the information. I have finally implemented my own timepicker. I had very specific requirements.
Thank you very much. Close this ticket if you want

@Kaizodo
Copy link

Kaizodo commented Mar 15, 2022

Same problem i am trying to change the format but its not working


import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './my/my.component';
import { MatDatepickerModule } from '@matheo/datepicker';
import { MatNativeDateModule } from '@matheo/datepicker/core';
// import { MatDatepickerModule } from '@angular/material/datepicker';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';


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


@NgModule({
  declarations: [
    MyComponent 
  ],
  exports: [
    MyComponent 
  ],
  imports: [
    MatNativeDateModule,
    MatDatepickerModule,
    CommonModule
  ],
  providers: [
    { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
    { provide: MAT_DATE_FORMATS, useValue: MY_FORMATS },
  ]
})
export class MyComponentModule { }

@neeraj-chhatani
Copy link

Hi,
I am also facing the same issue i want to display date in YYYY-MM-DD format and after using NativeDateModule it is not working.
@matheo if there is any solution pls do let us know

@banut1
Copy link

banut1 commented May 13, 2022

Facing the same, a working example would be gold.

@banut1
Copy link

banut1 commented May 18, 2022

After spending 2 days on this, I managed get it working. Here it is:

import { Platform } from '@angular/cdk/platform';
import { NativeDateAdapter } from '@matheo/datepicker/core';
import * as moment from 'moment';

export class CustomDateAdapter extends NativeDateAdapter {
  constructor(matDateLocale: string, platform: Platform) {
    super(matDateLocale, platform);
  }

  override format(date: Date, displayFormat: string): string {
    return moment(date).format(displayFormat);
  }
}

In my shared module:

import { DateAdapter, NativeDateModule } from '@matheo/datepicker/core';
import { MatDatepickerModule } from '@matheo/datepicker';
import { MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';
import { CustomDateAdapter } from './datepicker/custom-date-adapter';
import { Platform } from '@angular/cdk/platform';

...imports: [MatDatepickerModule, NativeDatepickerModule],
providers: [
{
      provide: DateAdapter,
      useClass: CustomDateAdapter,
      deps: [MAT_DATE_LOCALE, Platform],
    },
    {
      provide: MAT_DATE_FORMATS,
      useValue: {
        parse: {
          dateInput: 'DD/MM/YYYY',
          datetimeInput: 'DD/MM/YYYY, hh:mm A',
          timeInput: 'hh:mm A',
          monthInput: 'MM',
          yearInput: 'YYYY',
        },
        display: {
          dateInput: 'DD/MM/YYYY',
          datetimeInput: 'DD/MM/YYYY, hh:mm A',
          timeInput: 'hh:mm A',
          monthInput: 'MM',
          yearInput: 'YYYY',
          dateA11yLabel: 'DD/MM/YYYY',
          monthLabel: 'MMMM',
          monthDayLabel: 'DD MMM',
          monthDayA11yLabel: 'DD MMM',
          monthYearLabel: 'MMM YYYY',
          monthYearA11yLabel: 'MMM YYYY',
          timeLabel: 'hh:mm',
        },
      },
    }],

I also tried using material-moment-adapter and the DateAdapter that comes with it but I did not manage to, so basically wrote my own adapter that uses moment.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question / support Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants