Skip to content

Allow two digit year formatting #94

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

Open
brendonshih opened this issue May 11, 2021 · 12 comments
Open

Allow two digit year formatting #94

brendonshih opened this issue May 11, 2021 · 12 comments
Labels
enhancement New feature or request fresh

Comments

@brendonshih
Copy link

Currently the date picker only displays full four digit years (2021). I thought it might be a documentation oversight so I used the Unicode standard format year (yy) by trying M/d/yy and the date still comes out like 5/11/2021. It's pretty common for people to only want to display 2 digits due to space constraints so it would be nice if the formatter supported date formats like 5/11/21.

@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the stale label Feb 28, 2022
@voidname201

This comment was marked as spam.

@github-actions github-actions bot removed the stale label Mar 7, 2022
@github-actions

This comment was marked as outdated.

@brendonshih

This comment was marked as spam.

@github-actions github-actions bot removed the stale label Jun 13, 2022
@github-actions

This comment was marked as outdated.

@github-actions github-actions bot added the stale label Sep 12, 2022
@wojtekmaj
Copy link
Owner

Unlikely to happen, because we have no way of knowing which century we should assume.

new Date().getYear() returns 122 at the moment of writing. So we wouldn't be able to use even that!

@wojtekmaj wojtekmaj added the enhancement New feature or request label Sep 14, 2022
@brendonshih
Copy link
Author

brendonshih commented Sep 14, 2022 via email

@freerror
Copy link

When I looked at it did look there was quite a tight coupling between the representation and the actual value as stored in state. If you know a way @brendonshih (to separate the representation) you could submit a PR or provide an example (I would be happy to submit as a PR, but I don't have time to do it from scratch). Or worse case you could fork the project.

Just an observation that the tone of your message ("... it's weird how you're acting ...") came off quite rudely given that this is a free (and pretty awesome) project. I understand you've been waiting for some feedback for over year, but woktekmaj doesn't owe you a response.

@brendonshih
Copy link
Author

brendonshih commented Sep 15, 2022 via email

@freerror
Copy link

freerror commented Sep 15, 2022

Unlikely to happen, because we have no way of knowing which century we should assume.

new Date().getYear() returns 122 at the moment of writing. So we wouldn't be able to use even that!

Could the option be passed in to react-date-picker/src/shared/dateFormatter.js as part of the options object? Maybe it could be set with some new prop(s) from "numeric" to "2-digit" in react-date/picker/src/DateInput.jsx in the getter:

get formatDate() {
    const { maxDetail } = this.props;

    const level = allViews.indexOf(maxDetail);
    const formatterOptions =
      getFormatterOptionsCache[level] ||
      (() => {
        const options = { year: 'numeric' };  // <- e.g. make this configurable somehow (to "2-digit")
        if (level >= 2) {
          options.month = 'numeric';
        }
        if (level >= 3) {
          options.day = 'numeric';
        }

        getFormatterOptionsCache[level] = options;

        return options;
      })();

    return getFormatter(formatterOptions);
  }

It's not in the MDM page for Intl.DateTimeFormat but I found that option mentioned here

If I have time after work I'll hard code it and see if it breaks things.

@wojtekmaj
Copy link
Owner

What worries me more is "un-formatting" it. Like, "70" should be 1970 or 2070? How do we decide? Should we require additional century (number) or parseYearToFullYear (function) prop in this case? Guess? 🤔

@wojtekmaj wojtekmaj added fresh and removed stale labels Sep 15, 2022
@freerror
Copy link

freerror commented Sep 16, 2022

What worries me more is "un-formatting" it...

Hmm, if it's not currently the case, maybe the component could store the date in a "raw" format but a separate property in state could hold the value generated by the formatter. It's sounding like more work than I initially anticipated as I didn't think you'd need to switch between formats (e.g. to get the century) with only the formatted string of the date stored.

I'll update this comment if I spend more time looking at it, as I'm making a few assumptions. Just thinking out loud, I'm keen to check these assumptions:

  • Date is stored once in its localized format and this is used throughout the component tree (rather than having a representation separate from the "real" value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fresh
Projects
None yet
Development

No branches or pull requests

4 participants