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

Allow custom parse function for two digit years #1418

Closed
sbrunecker opened this issue Mar 17, 2021 · 4 comments
Closed

Allow custom parse function for two digit years #1418

sbrunecker opened this issue Mar 17, 2021 · 4 comments

Comments

@sbrunecker
Copy link
Contributor

sbrunecker commented Mar 17, 2021

Current Behavior

dayjs parses a two digit year as 19xx if the value is below 68, otherwise as 20xx.

Proposal

A possibility to configure this behavior would be nice. As a use case, we need to parse a two digit year as 19xx if the input is greater than the current year, otherwise as 20xx.

In moment this can be configured, e.g.:

moment.parseTwoDigitYear = function(yearString) {
    return parseInt(yearString) + 2000;
}
@iamkun
Copy link
Owner

iamkun commented Mar 18, 2021

maybe we can support this via a plugin

@sbrunecker
Copy link
Contributor Author

Would it be an idea to include it as a plugin option to the existing customParseFormat? So it could be configured like so:

dayjs.extend(customParseFormat, {
    twoDigitYearParseFn: yearString => (+yearString) + ((+yearString) > (+dayjs().format('YY')) ? 1900 : 2000)
  })

@iamkun
Copy link
Owner

iamkun commented Jun 2, 2021

fixed in #1421

@iamkun iamkun closed this as completed Jun 2, 2021
@maurer2
Copy link

maurer2 commented May 11, 2023

Hello, I noticed that in recent versions of dayjs versions, the pivot year seems to be different than the value mentioned above.
The default pivot year now seems to be 1950, instead of 1968 e.g.

dayjs('12/31/49').format() -> 2049-12-31T00:00:00+00:00
dayjs('01/01/50').format() -> 1950-01-01T00:00:00+00:00

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants