Bug: time manipulation is incorrect when crossing DST #465
Closed
Description
Because time manipulation methods like addHours
use set
methods like setHours
, crossing DST results in unexpected times.
Example:
In New York, DST starts on March 12 at 2am, skipping the hour between 2am and 3am. If you add 3 hours to the start of the day, it should return 4am (midnight + 3 hours, skipping 2am). date-fns returns 3am because it calls setHours(3)
.
Solution:
Instead of using setHours
, date-fns should add milliseconds to the given Date object:
new Date(date.getTime() + hours * 3600000)