angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.32k stars 6.73k forks source link

feat(datepicker): Add additional date adapter implementations #20599

Open jelbourn opened 4 years ago

jelbourn commented 4 years ago

This issue tracks creating additional date adapter implementations in addition to the existing MomentJS implementation. Libraries to consider include:

andriyor commented 4 years ago

Consider of using date-io

ahmadalfy commented 4 years ago

Voting for date-fns. Moment.js entered maintenance mode.

ghost commented 4 years ago

+1 @ahmadalfy go for date-fns! It's lightweight & fast.

mtpultz commented 3 years ago

I'm hoping by subscribing to this feature request for updates with Moment entering end of life that this doesn't mean I'm going to get a bunch of emails with everyones vote/opinion on which adapter implementation should be considered based on your own personal preference. Seems more logical to expand the list put forth by @jelbourn like @andriyor has since it wasn't included in the original list, but not voting individually through posts

bbarry commented 3 years ago

I am wondering if the material components repo is the right place for these things. IMO material should publish the api interface and encourage community contributions for the adapter services. Perhaps an endorsement program or something would be nice with a path to being mentioned in the documentation (a similar system could be useful for a number of projects that I've seen die stale code deaths from lack of updates because the projects fail to gain notoriety for the tiny enhancements they bring to the material ecosystem).

I'm assuming that the moment adapter exists due to use within Google. The only benefit I see to having it in this repo is to gain access to Google's robust build and package system (not always a benefit either; complexity always sucks) and maybe access to some test cases that aren't made public. If those aren't going to advantage these additional implementations then there seems little point to supporting them here (outside the notoriety angle which I think is a PR issue and not really a technical one).

AwesomeObserver commented 3 years ago

date-fns of course

andriyor commented 3 years ago

https://twitter.com/dmtrKovalenko/status/1254414963306909699?s=20

jelbourn commented 3 years ago

The current state of this is that we're still discussing whether this is something we want to maintain as a first-party (Angular team) thing, or instead defer to community implementations of different date adapters. We'll definitely weigh community feedback here.

MatthiasKunnen commented 3 years ago

@jelbourn I've once looked into creating a date-fns date adapter but decided against it because I deemed it a huge hassle to get the necessary testing framework into my own project. If there is a way the Angular team could provide some kind of base for date adapter libraries to build on I reckon third party implementations would be attainable.

KingDarBoja commented 3 years ago

@jelbourn I've once looked into creating a date-fns date adapter but decided against it because I deemed it a huge hassle to get the necessary testing framework into my own project. If there is a way the Angular team could provide some kind of base for date adapter libraries to build on I reckon third party implementations would be attainable.

There is some gist for a date-fns adapter, which I copied onto my project and worked like a charm, but in this case, there is already a base for building your own date adapter via https://material.angular.io/components/datepicker/overview#choosing-a-date-implementation-and-date-format-settings so not sure what you meant.

Cheers!

MatthiasKunnen commented 3 years ago

@KingDarBoja, making a quick implementation of a date adapter is pretty easy. Making a properly tested date adapter and keeping it in sync with changes in Angular is something else. The current MomentDateAdapter has the luxury of residing in the Angular Components source and can use the extensive testing framework that is available.

When a breaking change to the DateAdapter class is made, all third party date adapters will need to release an update to support this so you could have the problem that e.g. Angular 11 releases but your DateAdapter only supports up to Angular 10.

For testing, versioning, and breaking changes it will be easier if the code resides in this repository. That being said, maintaining a date adapter for every date library out there is not really what I believe that the Angular team should put their limited time into.

toamitc commented 3 years ago

Any path forward on these adapters? Need to replace moment-adapter with luxon-adapter?

MatthiasKunnen commented 3 years ago

My proposal is for the Angular team to create a package to test new date adapters, e.g. @angular/date-adapter-tests. The major version of this package would be kept in sync with the version of @angular/material it supports. This package would then be used by the community-made date adapters. This ensures consistency between all the date adapters and when changes to DateAdapter occur, date adapter maintainers can pull the latest @angular/date-adapter-tests and simply run the tests to verify their implementation is still correct or requires work.

Another possibility is instead of a package, the angular team could create a repository that community date adapters could fork and apply patches from.

mtpultz commented 3 years ago

PR for LuxonDateAdapter that has been open for over a year: https://github.com/angular/components/pull/14681#issuecomment-745170191. Finishing off this PR and merging it in would buy time for discussions, debates, opinions, etc; while not perpetuating the use of Moment in new and current projects.

KingDarBoja commented 3 years ago

Some community-made date-fns adapter has been done (seems based on the gist I shared before) as you can check out at https://github.com/sapozhnikovay/ngx-mat-datefns-date-adapter

bhanna1693 commented 3 years ago

Some community-made date-fns adapter has been done (seems based on the gist I shared before) as you can check out at https://github.com/sapozhnikovay/ngx-mat-datefns-date-adapter

This project is well done IMO. But the fact remains that an organization is unlikely to accept a third party date adapter solution unless it is held to the same testing standards as the MomentDateAdapter. Any updates/eta on the progress of the @angular/date-adapter-tests idea? I really like the sound of that

andreialecu commented 3 years ago

Just leaving this here in case people are looking for it. I took the PR that adds Luxon in https://github.com/angular/components/pull/14681#issuecomment-745170191 and published it to NPM a while ago at:

https://www.npmjs.com/package/ngx-material-luxon

It also fixes some bugs that were present in the original PR and adds a feature to set the first day of the week. Other contributions are welcome in: https://github.com/andreialecu/ngx-material-luxon

When a decision is made to add date adapters back to @angular/* I'd be happy to contribute the fixes back, and obsolete my package.

tuuling commented 3 years ago

https://www.npmjs.com/package/@angular/material-date-fns-adapter looks like something is brewing, any idea when this could be actually released?

jelbourn commented 3 years ago

It's riding the train to v13 stable

EusebiuDev commented 2 years ago

Any updates on the Day.js adapter?

JohanHeyvaert commented 2 years ago

Great that there is finally an adapter for date-fns.

Now it would be even more great if it would offer the same features.

For the moment (no pun intended) we are missing the option to disable the automatic timezone conversion. We need this for certain date fields (e.g. a birthdate). With the MomentDateAdapter this could be done as follows:

  imports: [MatDatepickerModule, MatMomentDateModule],
  providers: [
    {provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: {useUtc: true}}
  ]

It's possible that I've missed something and that this is already possible. Please let me know how if so.

Thanks in advance!

the-ult commented 2 years ago

Would be great if the documentation of https://material.angular.io/components/datepicker/overview#choosing-a-date-implementation-and-date-format-settings would have an example of the matDateFnsModule implementation.

It seems like it is different than the MatMomentDateModule (When looking at the .spec file

[Edit] Looking at the source: https://github.com/angular/components/blob/main/src/material-date-fns-adapter/adapter/index.ts I Would expect this to be enough:

app.module.ts

import { nl } from 'date-fns/locale';
import { MatDateFnsModule } from '@angular/material-date-fns-adapter';

@NgModule({
  declarations: [],
  imports: [
    MatDateFnsModule
  ]
  providers: [
    { provide: LOCALE_ID, useValue: 'nl-NL' },
    { provide: MAT_DATE_LOCALE, useValue: nl },
  ],

my-date.component.ts

@NgModule({
  declarations: [],
  imports: [
 //    MatDateFnsModule // NOT needed here again..
  ],

Correct?

Retry9997 commented 2 years ago

Adding to that documentation issue, I would like to note that it isn't clear how to resolve a particular locale object for MAT_DATE_LOCALE in projects that support multiple locales. Is this only possible by including all possible locale objects and mapping at runtime? An example or just a small footnote would be helpful when this is being documented.

Tyler-V commented 2 years ago

Day.js please :)

Blackscr33n commented 1 year ago

Dayjs should have been the top prio as it should replace moment. Sad to see that this is the only one missing.

yanzacheus commented 1 year ago

Hi, is it possible to know if it is still in progress, any status quo ? I chose to work with dayjs, but if it's no more in the roadmap of angular material or will be released much later, perhaps it's better for me to try another lib like date-fns, even if the migration is less simple.

bgpedersen commented 1 year ago

Any updates on dayjs adapter ? I tried to make it myself, but I can't make it work, and the docs doesn't give away any example of how it should be done. Would also be nicer to use a official supported adapter for dayjs, instead of a custom made one.

hakimio commented 1 year ago

They have decided against adding any new officially supported date adapters a long time ago. So, if you really need it, you have to check their implementation and do it yourself.

DerHerrGammler commented 1 year ago

Are their any work on dayjs these days? Dayjs is much smaller than the other options so it would be very great if we could get aimplementation of it.