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

fix: correctly parse years between 0 and 99. fixes #2550 #2551

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

hanayashiki
Copy link

@hanayashiki hanayashiki commented Jan 11, 2024

In the related issue, dayjs('0099-11-11').get('year') gets 1999 instead of 99.

However, in vanilla Date, it parses it correctly: new Date('0099-01-01').getUTCFullYear() === 99.

The reason lies in how we create the underlying Date object.

In new Date() of parseDate, when the first parameter is less than 99 and greater than 0, according to MDN:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_components_and_time_zones

Note: Some methods, including the Date() constructor, Date.UTC(), and the deprecated getYear()/setYear() methods, interpret a two-digit year as a year in the 1900s. For example, new Date(99, 5, 24) is interpreted as June 24, 1999, not June 24, 99. See Interpretation of two-digit years for more information.

When d[1] is between 0 and 99, the year is interpreted as 1900 + d[1], thus we get 1999 instead of 99.

new Date(d[1], m, d[3]
        || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)

To fix this, I don't think it is possible to create a Date object with the correct year directly, so I add one line to set the year correctly. This is a little ugly, and better ideas welcome.

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

Successfully merging this pull request may close these issues.

None yet

1 participant