简体   繁体   中英

Is there any way to compare negative 0 ie. -0 which is returned from Math.sign(-0)?

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM