question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect year when adding time for years before 100

See original GitHub issue

Given 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:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
icambroncommented, Feb 4, 2019

Fixed in c16d7d1

1reaction
jjacobson93commented, Feb 4, 2019

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:

if (obj.year < 100 && obj.year >= 0) {
  d = new Date(d);
  d.setUTCFullYear(d.getUTCFullYear() - 1900);
}

Let’s assume we add 657438 days (the number of days between the end of 0099 and 1900) to 0099-12-31, so obj.day would be 31 + 657438, resulting in an obj like this:

{ year: 99, month: 11, day: 657469, hour: 0, minute: 0, second: 0, millisecond: 0 }

Date.UTC(...) will see obj.year as 1999 and obj.day will cause an overflow resulting in the year 3800 (657438 ≈ 1800 years). Thus, d.getUTCFullYear() - 1900 would equal 1900, which is the expected year.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found