moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.05k stars 730 forks source link

`twoDigitCutoffYear` not working as expected #1595

Closed 238855 closed 3 months ago

238855 commented 4 months ago

Describe the bug This seems like a documentation error.

Set the cutoff year after which a string encoding a year as two digits is interpreted to occur in the current century.

The line of code that is supposed to acheive this is } else return year > Settings.twoDigitCutoffYear ? 1900 + year : 2000 + year; (Link)

To Reproduce See RunKit links for Actual vs Expected behaviour.

Actual vs Expected behavior

Expected If I set the cuttoff year to 0 then I expect all years in range 00 - 99 to be interpreted as 2000 - 2099 (current century)

Actual If I set the cuttoff year to 0 then I all years in range 00 - 99 are interpreted as 1900 - 1999 (previous century) https://runkit.com/238855/65defe178dd6300008b6837f

Workaround To have all two-digit years be interpreted as current century, use Settings.twoDigitCutoffYear = 99 https://runkit.com/238855/65deffb6da3d3000087906dc

icambron commented 3 months ago

Yeah, the docs are just wrong here. It should say this:

Set the cutoff year for whether a 2-digit year string is interpreted in the current or previous century. Numbers higher than the cutoff will be considered to mean 19xx and numbers lower or equal to the cutoff will be considered 20xx.

This makes sense and is the right thing to do (e.g. '89 means 1989 but '17 means 2017) but the docs are backward.

I've fixed the doc string and examples in 73c44388 though it won't make it to the site until we do another release.

Thanks for the report!