Skip to content

date-adapter parse #4769

Closed
Closed

Description

@fxck

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

Activity

  1. mmalerba commented on May 24, 2017

    @mmalerba
    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).

  2. fxck commented on May 24, 2017

    @fxck
    ContributorAuthor

    @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?

  3. mmalerba commented on May 24, 2017

    @mmalerba
    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?

  4. fxck commented on May 24, 2017

    @fxck
    ContributorAuthor

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

  5. areijngoudt commented on May 25, 2017

    @areijngoudt

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

  6. fxck commented on May 25, 2017

    @fxck
    ContributorAuthor

    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.

  7. robindijkhof commented on May 27, 2017

    @robindijkhof
    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?

  8. fxck commented on May 27, 2017

    @fxck
    ContributorAuthor

    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.

  9. robindijkhof commented on May 27, 2017

    @robindijkhof
    Contributor

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

  10. fxck commented on May 27, 2017

    @fxck
    ContributorAuthor

    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.

  11. angular-automatic-lock-bot commented on Sep 5, 2019

    @angular-automatic-lock-bot

    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.

  12. locked and limited conversation to collaborators on Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions