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
Incorrect day/week calculations across daylight saving boundaries #3519
Comments
Hello, in which file is this issue located? |
@matejtuke if you are referring to where the calculation happens: it's here https://github.com/date-fns/date-fns/blob/main/src/addDays/index.ts#L34 |
can you assign this to me please? |
@matejtuke i'm not a mod. I guess the best thing to do would create a pull request and reference this issue |
Hi @campbellwmorgan , I guess I found the source of the problem. You said that when you did the calculation, in the result was an extra hour substracted. I think that the problem is not in the calculations, but in the As you can see in the code bellow, you dont have to do any type of calculation and the result could be displayed in 2 forms. const end_of23 = new Date(2023,11,31)
console.log("End of 2023 > " + end_of23)
console.log("End of 2023 > " + end_of23.toISOString())
console.log("End of 2023 > " + end_of23.toUTCString()) and the output:
So, it does not matter if you do calculation or not, the source of the problem is just in the displaying the date and time, which has nothing to do with the actual code of the project. |
@matejtuke I'm afraid it's not that. If you look at the output of my example above, you'll see that I'm only using If you execute my above example on a machine on UTC time and then execute the same code on a machine on CET time you will observe a different output. It's also worth pointing out that you are not using |
Good Point, thanks |
I have been debugging a snapshot unit test involving subtracting days from a UTC date. When adding / subtracting days where the result crosses a daylight saving boundary and the timezone of the machine performing this calculation is in a timezone with daylight saving, an extra hour is subtracted to the underlying UTC date.
Example
When the following is executed on a machine with a non-UTC timezone (in my case CEST)
The expected result of
fiveDaysLess
should be2021-10-31T00:00:00.000Z
However, when executed on a machine with UTC timezone, the results for
fiveDaysLess
returns2021-10-31T00:00:00.000Z
correctly.Having looked into the implementation of
subDays
, I see that it uses thesetDate
andgetDate
javascript API underneath.Would
subDays
and thusaddDays
,addWeeks
,subWeeks
not be better implemented by adding / subtracting1000*60*60*24
to the underlying raw date value?I guess this is partly an expectation of how the API should work in localised scenarios.
The text was updated successfully, but these errors were encountered: