You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please note: MdNativeDateModule is based off of the functionality available in JavaScript's native Date object, and is thus not suitable for many locales. One of the biggest shortcomings of the native Date object is the inability to set the parse format. We highly recommend using a custom DateAdapter that works with the formatting/parsing library of your choice.
Setting the locale fixed the problem. Not sure if it is working correclty though. I also tried setting the local in angular { provide: LOCALE_ID, useValue: 'nl' } But that didn't work. It would be nice if the datapicker used that value to.
@fxck, maybe the component is in it's early stages, but a little improvement in its docs at material.angular.io would be nice. Things like this.dateAdapter.setLocale('nl') can save a lot of headaches. Or maybe examples like you provided above.
I'm not sure why it is not mentioned in the docs, perhaps because what I said, setting a locale using the adapter material provides "breaks" it. Ask @mmalerba.
@fxck, it has at least one side efect (or a bug?): once you set the date and the input is updated, there's no garantee that the next time the datepicker is shown it will show the right period.
Here in Brazil, for example, the default date format is dd/MM/yyyy. If I just use this.dateAdapter.setLocale('pt-br') and then select, for exemple, April 12th 2017, it will show me, correctly, 12/04/2017. The next time I choose a date, it will show December in the date picker (because the day I chose before is 12).
If I select a day greater then twelve, it shows me the current month.
Reacted by Rimvydas Naktinis, markus-heinisch, Jair Avilés, João Luiz Schlickmann Kestering, cjewiss and tomwalls
@stevenmi , there's no need for the providers: [NativeDateAdapter], you can take it away (and do the same to the injection of NativeDateAdapter in the AppModule class, unless you were just trying something else).
In the constructor of the App class, inject DateAdapter<Date>, just like it's shown in the example provided by fxck's plunker: https://plnkr.co/edit/o6ZuFmJ3Qk3Rr6Uuz8cA?p=preview (don't forget to import the DateAdapter module from Material).
@mmalerba I would like to add something into the consideration.
I am also in one of the dd/mm/yyyy locales and therefore went and created a date adapter for momentjs since I already use this library in my project.
This all works fine as long as the locale stays the same within the application.
The parse function receives the format string from the override of MdDateFormats but this is a constant.
Now what I am trying to do, is to make it as flexible enough to be able to cater for other locales with different formats as well but don't really had a great idea on how to do that apart from ignoring the parse format altogether and handle this in the adapter itself.
@HopScotch47 hey timo, Im currently trying to extend date adapter to use moment js in an application. Im struggling because of the lack of documentation and some weird errors. Were you able to made your custom implementation? Would you mind sharing some tips with me maybe?
Unfortunately the current situation is pretty much that you have to create a custom DateAdapter for locales that use dd-mm-yyyy format. However we are going to do a couple things to make this better in the near future:
Activity
fxck commentedon May 27, 2017
That depends on the locale you use.
robindijkhof commentedon May 27, 2017
My locale is nl, but M03 doesn't seem like a correct month
fxck commentedon May 27, 2017
Hard to tell, you didn't provide a plunker.
robindijkhof commentedon May 27, 2017
https://plnkr.co/edit/1mhwobNRXjgUr3KM6U8e?p=preview
fxck commentedon May 27, 2017
It doesn't seem like you are setting your locale to nl anywhere in that plunker.
robindijkhof commentedon May 27, 2017
That's right and yet I am seeing M03. Chrome 58 and Chrome mobile. Any idea?
fxck commentedon May 27, 2017
Maybe try actually setting your locale to nl?
fxck commentedon May 27, 2017
https://plnkr.co/edit/o6ZuFmJ3Qk3Rr6Uuz8cA?p=preview
https://github.com/angular/material2/blob/master/src/lib/core/datetime/date-adapter.ts#L155
fxck commentedon May 27, 2017
Its gotta be said though, that the datepicker might not work correctly after you do that.
you can try and implement your own adapter
robindijkhof commentedon May 27, 2017
Setting the locale fixed the problem. Not sure if it is working correclty though. I also tried setting the local in angular
{ provide: LOCALE_ID, useValue: 'nl' }But that didn't work. It would be nice if the datapicker used that value to.julianobrasil commentedon May 27, 2017
@fxck, maybe the component is in it's early stages, but a little improvement in its docs at material.angular.io would be nice. Things like this.dateAdapter.setLocale('nl') can save a lot of headaches. Or maybe examples like you provided above.
fxck commentedon May 27, 2017
I'm not sure why it is not mentioned in the docs, perhaps because what I said, setting a locale using the adapter material provides "breaks" it. Ask @mmalerba.
julianobrasil commentedon May 27, 2017
@fxck, it has at least one side efect (or a bug?): once you set the date and the input is updated, there's no garantee that the next time the datepicker is shown it will show the right period.
Here in Brazil, for example, the default date format is dd/MM/yyyy. If I just use this.dateAdapter.setLocale('pt-br') and then select, for exemple, April 12th 2017, it will show me, correctly, 12/04/2017. The next time I choose a date, it will show December in the date picker (because the day I chose before is 12).
If I select a day greater then twelve, it shows me the current month.
robindijkhof commentedon May 27, 2017
So basically English or wrting your own dateAdapter is the way to go?
1 remaining item
julianobrasil commentedon May 27, 2017
Ok. I took the code of material adapter and made some changes to fit to my particular case (just a small one at the parse function in the adapter).
julianobrasil commentedon May 27, 2017
@stevenmi , there's no need for the providers: [NativeDateAdapter], you can take it away (and do the same to the injection of NativeDateAdapter in the AppModule class, unless you were just trying something else).
In the constructor of the App class, inject
DateAdapter<Date>, just like it's shown in the example provided by fxck's plunker: https://plnkr.co/edit/o6ZuFmJ3Qk3Rr6Uuz8cA?p=preview (don't forget to import the DateAdapter module from Material).Depending of the locale, it may or may not work right as said in this #4358 (comment). If this is your case, you'll have to build your own adapter. See your modified plunker: https://plnkr.co/edit/v24Y7pFNpAVZpM1UsTQH?p=preview
mmalerba commentedon May 30, 2017
Sounds like there are two things to do here:
setLocaleLOCALE_IDHopScotch47 commentedon Jun 5, 2017
@mmalerba I would like to add something into the consideration.
I am also in one of the dd/mm/yyyy locales and therefore went and created a date adapter for momentjs since I already use this library in my project.
This all works fine as long as the locale stays the same within the application.
The parse function receives the format string from the override of MdDateFormats but this is a constant.
Now what I am trying to do, is to make it as flexible enough to be able to cater for other locales with different formats as well but don't really had a great idea on how to do that apart from ignoring the parse format altogether and handle this in the adapter itself.
Any thoughts?
mmalerba commentedon Jun 6, 2017
@HopScotch47 with moment, just use one of the formats that supports multiple locales: https://momentjs.com/#multiple-locale-support If that's not good enough please open a separate issue describing your situation
jotatoledo commentedon Jul 13, 2017
@HopScotch47 hey timo, Im currently trying to extend date adapter to use moment js in an application. Im struggling because of the lack of documentation and some weird errors. Were you able to made your custom implementation? Would you mind sharing some tips with me maybe?
julianobrasil commentedon Jul 13, 2017
@jotatoledo, take a look at #675 (comment) from @arlowhite.
jotatoledo commentedon Jul 13, 2017
@julianobrasil Thanks for the reply! I just finished my own version ahah, but it will come handy to take some ideas from the one you pointed out 😄
mmalerba commentedon Jul 25, 2017
Unfortunately the current situation is pretty much that you have to create a custom
DateAdapterfor locales that usedd-mm-yyyyformat. However we are going to do a couple things to make this better in the near future:NativeDateAdaptermore robust and able to handle all locales (see datepicker: Integrate new angular i18n API into NativeDateAdapter #6030)DateAdapterswith Angular Material to support Moment.js and date-fns out of the box (see feat(datepicker): add aditional DateAdapters #5972)angular-automatic-lock-bot commentedon Sep 6, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.