1

I'm using JavaScript Date() utility in my code to get the today's tasks by comparing two different dates.

otApp.TaskPanelUtils.getDaysDiff = function(task)
    {
        var current = new Date();
        var taskDueDate = new Date(task.unformattedDueDate())
        return Math.trunc((current.getTime()-taskDueDate.getTime())/otApp.TaskPanelUtils.oneDay);
    }

var daysDiff = otApp.TaskPanelUtils.getDaysDiff(taskItem);

if(daysDiff==0 && Math.sign(daysDiff)==0)
{
    tempItems.push(taskItem);
}

The above code is working even if I get "-0" negative 0 as result of getDaysDiff().

I want to fill tempItems only in case of positive "0".

Math.sign(-0) will return -0, then how come comparision with "-0" or -0 is not working?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Raju Putchala
  • 96
  • 1
  • 7

1 Answers1

1

The above code is working even if I get "-0" negative 0 as result of getDaysDiff().

Because 0 and -0 evaluate to 0.

Math.sign(-0) == Math.sign(0) //outputs true

0 == -0; //outputs true

0 === -0 //outputs true

String(-0) //outputs "0"

Edit:

I would suggest to simplify/modify the function so that it returns a boolean

otApp.TaskPanelUtils.getDaysDiff = function(task)
{
    var current = new Date();
    var taskDueDate = new Date(task.unformattedDueDate())
    return ((current.getTime()-taskDueDate.getTime())/otApp.TaskPanelUtils.oneDay) > 0;
}

And use it as

if(otApp.TaskPanelUtils.getDaysDiff(taskItem))
{
    tempItems.push(taskItem);
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Hi @gurvinder372, Math.sign(-0) will return -0 and Math.sign(0) will return 0, according to the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign, how to consume Math.sign(-0) for comparing negarive zero. – Raju Putchala Mar 21 '16 at 11:46
  • @RajuPutchala `Object.is(0,-0)` should give `false`. However, I don;t think you need to do that. Just return true/false from getDiff method if value < 0. – gurvinder372 Mar 21 '16 at 11:48
  • Hi @gurvinder372, the above suggested code is working fine..thanks you – Raju Putchala Mar 21 '16 at 12:20
  • @RajuPutchala Glad to help, Please accept and upvote the best answer that has helped you http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – gurvinder372 Mar 21 '16 at 12:25