Skip to content

addDays() behaves different in Chrome and Firefox (not working with decimal number) #746

Closed
@MickL

Description

Lets say i have the time of now: 16th may 16:44 and using addDays with an uneven number like 1.3 i expect the function to add 1.3 days to 16:44 resulting in something around 16th may 21:01 with the exact amount of minutes an seconds.

This works fine in Chrome. But using Firefox addDays does NOT change the minutes/seconds/ms. The reason is the usage of Date.setDate()

Example:
https://codepen.io/MickL/pen/GdYrvw

Tests should be updated to include date with hours/minutes and an uneven number of days.

Activity

leshakoss

leshakoss commented on May 16, 2018

@leshakoss
Member

@MickL I would say it should Math.floor the amount of days. So it sounds like a bug to me

MickL

MickL commented on May 16, 2018

@MickL
Author

Can you confirm addDays sets minutes, hours and ms to 0 on Firefox but not on Chrome?

MickL

MickL commented on May 16, 2018

@MickL
Author

I think the problem is that addDays() is using Date.setDate. I tried on Chrome with a float it works, but Firefox does not change the hours/minutes:

const a = new Date();
a.setDate(a.getDate() + 1.3);
console.log(format(a, 'YYYY-MM-DD HH:mm'));

Its not a bug but i think addDays() should be changed to not use Date.setDate() which does not work with decimal numbers.

Created an example here:
https://codepen.io/MickL/pen/GdYrvw

changed the title addDays() behaves different in Chrome and Firefox addDays() behaves different in Chrome and Firefox (not working with decimal number) on May 16, 2018
MickL

MickL commented on May 16, 2018

@MickL
Author

Workaround: Use addMinutes / addSeconds instead

kossnocorp

kossnocorp commented on May 25, 2018

@kossnocorp
Member

This is looks like a Chrome bug if I read the spec correctly.

I believe that addDays shouldn't change any units except days. The passed argument should be rounded using floor. We should apply this not only to addDays but also to all add/sub functions for consistency sake.

@MickL not sure what was your original intention when you were adding 1.3 days, but please use addHours/addMinutes/etc. for that.

kossnocorp

kossnocorp commented on May 25, 2018

@kossnocorp
Member

I've opened an issue in the Chromium issue tracker: https://bugs.chromium.org/p/chromium/issues/detail?id=846723

added a commit that references this issue on Apr 13, 2024
daddf06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      addDays() behaves different in Chrome and Firefox (not working with decimal number) · Issue #746 · date-fns/date-fns