簡體   English   中英

嘗試比較兩個負數的問題

[英]Problems trying to compare two negative numbers

我在下面有這個 function,我正在嘗試比較負數,我使用的是 unix 時間,所以時間差是負數,因為日期已經過去,但我仍然想使用它,當 within5Days 變量為負四時( -4)within5DaysBoolean 變量沒有設置為false,它仍然被設置為true,我不知道我在這里缺少什么,任何幫助

function timeDifference2(next, now) {
    var sPerMinute = 60;
    var sPerHour = sPerMinute * 60;
    var sPerDay = sPerHour * 24;
    var sPerMonth = sPerDay * 30;
    var sPerYear = sPerDay * 365;
    var sPerTwoDays = sPerDay * 2;

    var elapsed = next - now;
    return Math.floor(elapsed / 3600);
}


var within5Days = timeDifference2(unixTime, currentUnixTime);
if (within5Days < 120 && within5Days > -2) {
    var within5DaysValue = within5Days;
    var within5DaysBoolean = true;
} else if (within5Days > 120 || within5Days < -2) {
    var within5DaysValue = within5Days;
    var within5DaysBoolean = false;
}

您可以使用 parseFloat 嘗試如下

if((parseFloat(within5Days) < 120 && parseFloat(within5Days) > -2)) {
     var within5DaysValue = within5Days;
     var within5DaysBoolean = true;
} else if ((parseFloat(within5Days) > 120) || parseFloat(within5Days) < -2)) {
     var within5DaysValue = within5Days;
     var within5DaysBoolean = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM