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

date-adapter parse #4769

Closed
fxck opened this issue May 24, 2017 · 11 comments
Closed

date-adapter parse #4769

fxck opened this issue May 24, 2017 · 11 comments

Comments

@fxck
Copy link
Contributor

fxck commented May 24, 2017

I'm trying to wrap my head around what's the best approach, I still think it's missing a proper concept of parsers / formatters.

Data coming from the API is going to be in the ISO format 2017-05-24T10:38:28.434Z, now I have <input /> with formControl, if I implemented custom parse function, which would take the dateFormats.parse.dateInput, which should (I assume) be the format of the localized date my consumer actually write in the input and pass it to, in my case date-fns', parse function. Now the question is, what do I do with the format I receive from the API? Do I have to actually format it to the localized format before patchValueing it to my formControl, because otherwise the initial value, which would be in ISO format, would be different from subsequent edit by the user (those will be in the localized format).

cc @mmalerba

@mmalerba
Copy link
Contributor

I would recommend creating a date object from the API's response and patching that directly into the formControl (no need to reformat it in a new format).

I'll also mention that part of the reason for having separate parse and display formats is so you can support multiple parse formats, for example, you might have:

dateFormats.parse.dateInput = ['LL', 'YYYY-MM-DDTHH:mm:ssZ'];
dateFormats.display.dateInput = 'LL';

(with an appropriate DateAdapter that knows how to handle that).

@fxck
Copy link
Contributor Author

fxck commented May 24, 2017

@mmalerba I'm not sure that would work.. setting of the initial value, which happens here uses the same parse format as the function that is called on input change.

Or you are saying that my custom parse function should handle both native Date as well as my localized input?

@mmalerba
Copy link
Contributor

Yes, the NativeDateAdapter works with string, Date, and number. In general I think all implementations should at least support string and an instance of whatever date implementation that adapter operates on. It would probably be good to call this out in the method docs, would you mind creating a PR?

@fxck
Copy link
Contributor Author

fxck commented May 24, 2017

Sure. I'll just finish this date-fns adapter first.

@areijngoudt
Copy link

areijngoudt commented May 25, 2017

@fxck can you share your date-fns based implementation when you finished it? I would like to use it :)

@fxck
Copy link
Contributor Author

fxck commented May 25, 2017

I'm using date-fns version that is not even public yet (the current version doesn't support parse with custom format) and I'm also using a custom copy of all datepicker / date-formats / adapter stuff, all variously modified since I needed to extend the datepicker with a datetimepicker mode.. it's not really easily sharable.

image image

But you can try and mess with it if you want, here's the build of the 2.0.0 of date-fns https://github.com/fxck/dfns-builds and here's the adapter https://gist.github.com/fxck/efe4ccff13d99ee9a2dcf82365909168 you'll have to remove the extra time related methods and fix some imports.

@robindijkhof
Copy link
Contributor

@fxck, how did you managed the get the timepicker? Is it a matter of writing your own dataAdapter or is it an undocumented feature?

@fxck
Copy link
Contributor Author

fxck commented May 27, 2017

I made it. By extending the current datepicker. It also means I have to maintain my own version and manually apply all of the PRs, which sucks. But I simply couldn't proceed without a datetimepicker.

@robindijkhof
Copy link
Contributor

Is it published somewhere and may I use it? Or is it supposed to be a PR someday?

@fxck
Copy link
Contributor Author

fxck commented May 27, 2017

Nah, it's pretty crude. There's probably a private material design spec for a datetimepicker that the official version will be based on one day, so I'd be waste of time.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants