24

I am using MomentJS to calculate the difference between two times. The weird thing is about this library is the difference between today and today is 0. The difference between today and tomorrow is -0.

My question is, how do I differentiate between 0 and -0. It seems JavaScript treats them the same.

So for example if I write the following code:

if (tomorrow === -0)
    console.log('It is tomorrow!');
else if (tomorrow === 0)
    console.log('It is today!');

Here's an example on JSFiddle as to how it handles the returned values (I am in Australia so depending on where you are in the word you may have to adjust the today and tomorrow dates)

Ben Sinclair
  • 3,896
  • 7
  • 54
  • 94
  • 2
    For me, `tomorrow` is showing up as `-1`, not `-0`. (via your fiddle) – Pointy Jul 23 '15 at 23:25
  • about 0 and -0 problem, can you take a look at this http://stackoverflow.com/questions/7223359/are-0-and-0-the-same – cycopepe Jul 23 '15 at 23:26
  • 1
    Where are you, like physically on Planet Earth right now? – Pointy Jul 23 '15 at 23:26
  • When I try it here in my continent, with `23` and `24` I get your result. However, the difference in days is really zero for both of those, since it's currently the 23rd (so zero days) and it's less than half a day until it's the 24th) which rounds to zero days too. In JavaScript, `0` is `===` to `-0`. – Pointy Jul 23 '15 at 23:28
  • 3
    Shouldn't the difference between today and tomorrow be 1? – RobG Jul 23 '15 at 23:37
  • @RobG I agree. I've opend an issue on their [GitHub](https://github.com/moment/moment/issues/2503) – Ben Sinclair Jul 23 '15 at 23:52

2 Answers2

49

1/val > 0 will do what you want. Returns true for positive and false for negative.

This works because 1/-0 returns negative infinity, and 1/0 returns positive infinity, which are then comparable. You could also do something like 1/val == Infinity.

Shelvacu
  • 4,245
  • 25
  • 44
  • 2
    As of ES2015, you can also check for equality with Object.is(). It also checks special cases (e.g. Object.is(-0, -0) is true while Object.is(0, -0) is false) – Jordan Stubblefield Jan 29 '19 at 17:30
11

As pointed out in this Stack Overflow question (and in the comments), in JavaScript +0 === -0 indeed evaluates to true by design.

You can use Infinity/-Infinity to see a difference (technically, +0/-0 might not be the only numbers to produce infinite values, so I left the additional check in):

var positiveZero = +0;
var negativeZero = -0;

console.log(positiveZero === 0 && 1/positiveZero === Infinity); // true
console.log(positiveZero === 0 && 1/positiveZero === -Infinity); // false

console.log(negativeZero === 0 && 1/negativeZero === Infinity); // false
console.log(negativeZero === 0 && 1/negativeZero === -Infinity); // true

JSFiddle

See also this blog post for more details and yet another solution with ECMAScript 5:

function isNegative0(x) {
   if (x!==0) return false;
   var obj=Object.freeze({z:-0});
   try {
      Object.defineProperty(obj,'z',{value:x});
   } catch (e) {return false};
   return true;
}
Community
  • 1
  • 1
Marvin
  • 13,325
  • 3
  • 51
  • 57