Incorrect year when adding time for years before 100
See original GitHub issueGiven a DateTime
with a year that is before 100, trying to add time that crosses to the next year results in the same year.
For example,
const a = DateTime.local(99, 12, 1) // 0099-12-01
const b = a.plus({ month: 1 }) // 0099-01-01 (should be 0100-01-01)
a.year === b.year // true
I dug into the code and it turns out there’s a special case here that sets the UTC full year using the JavaScript Date object, because it would result in the year 2000 instead of 100.
However, it uses the “original” year (in this case 99), because it does not take into account the month, which is 12 (January of the next year).
This is also incorrect since that same code doesn’t take into account days greater than the last day of the month.
DateTime.local(99, 12, 31).plus({ day: 1 }) // 0099-01-01
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Excel keeps changing time or dates to incorrect format or ...
I'm trying to create an ongoing sheet that includes dates and time. I enter the time as this example: 5:00 and it changes...
Read more >Time formatting and storage bugs - Wikipedia
In computer science, time formatting and storage bugs are a class of software bugs that may cause time and date calculation or display...
Read more >Excel Date and Time - Everything you need to know
It goes by the rule that dates with years 29 or before, are treated as 20xx and dates with the year 30 or...
Read more >The Definitive Guide to Using Dates and Times in Excel
If we want to represent a specific time and date, we can add the two functions ... to measure how much time has...
Read more >YEAR function convert date to year in Excel - Ablebits
Today, we are going to focus on a bigger time unit and talk about calculating years in your Excel worksheets.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Fixed in c16d7d1
You would still check if
obj.year
is in between [0, 100) in the if-condition, but offset the resulting year by 1900 inside the block. So that block becomes:Let’s assume we add 657438 days (the number of days between the end of
0099
and1900
) to0099-12-31
, soobj.day
would be31 + 657438
, resulting in anobj
like this:Date.UTC(...)
will seeobj.year
as1999
andobj.day
will cause an overflow resulting in the year3800
(657438 ≈ 1800 years). Thus,d.getUTCFullYear() - 1900
would equal1900
, which is the expected year.