Skip to content
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

differenceInYears() returns -0 when comparing the same date #692

Closed
hanshenrik opened this issue Feb 16, 2018 · 6 comments
Closed

differenceInYears() returns -0 when comparing the same date #692

hanshenrik opened this issue Feb 16, 2018 · 6 comments
Assignees
Labels
Milestone

Comments

@hanshenrik
Copy link

differenceInYears('2018-02-01',` '2018-02-01')
// -0

differenceInYears(new Date(), new Date())
// -0

differenceInYears('2018-02-02', '2018-02-01')
// 0

Shouldn't the same year always be returned as a "positive" zero value?

Solvable by using Math.abs(value) if the value is 0. Perhaps a bigger discussion if all values should be returned as positive numbers.

@ghostd
Copy link

ghostd commented Apr 30, 2018

I think we should have a positve 0 (as proposed by the PR of armenavetisyan), but keep the sign for the non-zero results. thi can be used to know if we are before/after an other date.

@webpapaya
Copy link

I think this is not only an issue with differenceInYears but some other differenceIn* functions as well.

{
  differenceInCalendarDays: 0,
  differenceInCalendarISOWeeks: 0,
  differenceInCalendarISOYears: 0,
  differenceInCalendarMonths: 0,
  differenceInCalendarQuarters: 0,
  differenceInCalendarWeeks: 0,
  differenceInCalendarYears: 0,
  differenceInDays: -0,
  differenceInHours: 0,
  differenceInISOYears: -0,
  differenceInMilliseconds: 0,
  differenceInMinutes: 0,
  differenceInMonths: -0,
  differenceInQuarters: -0,
  differenceInSeconds: 0,
  differenceInWeeks: -0,
  differenceInYears: -0,
}

I think -0 should never be returned by any of the functions. It would be nice if @ArmenAvetisyan could also take the other functions in his PR into account. Thanks.

@kossnocorp
Copy link
Member

Hm, on one hand, that's an annoying inconsistency, but on another hand -0 and 0 for JS are the same thing:

0 == -0
//=> true

0 === -0
//=> true

JSON.stringify(-0)
//=> "0"

I think that it doesn't make sense to add abs everywhere just for sake of it unless there are real concerns.

@leshakoss WDYT?

@kossnocorp kossnocorp added 🤔 Feedback wanted Community feedback is required 🐛 Bug and removed 🤔 Feedback wanted Community feedback is required labels May 9, 2018
@kossnocorp kossnocorp added this to the v2.0.0 milestone May 9, 2018
@kossnocorp
Copy link
Member

@leshakoss figured out that the problem is in Math.round. I still not sure how this is a problem, but since it could be fixed using simple || 0 and @leshakoss is so into it, here is my 👍 for making it consistent across the code base.

@leshakoss
Copy link
Contributor

It was not Math.round, I just tried to guess without looking in the code 😅

@kossnocorp
Copy link
Member

@leshakoss nice try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants