如何计算两次之间的剩余量以获得下一次减少JavaScript?

时间:2018-05-30 13:24:38

标签: javascript timestamp

我用prayertimes.org建立了祈祷时间。现在我需要得到下一个祷告,并且在即将到来的祷告之前需要多少小时,几分钟,几秒钟。我已将日期时间转换为24种格式,以便能够获得时间之间的距离,并检查两次之间的时间是否为即将到来的祷告。但是,我没能得到正确的。

祈祷时间示例(现在时间是6:50 - Fajr是3:05 - Dhuhr是13:10 ......等等) 我已经在上午或下午将时间转换为24甲酸盐。

// Update the count down every 1 second
var x = setInterval(function() {


    if (countDownDate > IshaTimeAF && countDownDate <= FajrTimeAF) {
        // Find the distance between now an the count down date
        var distance = FajrTimeAF - countDownDate;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        document.getElementById("ComingSoon").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
        //display = document.querySelector('#ComingSoon') + "UPCOMING PRAYER FAJR";

    }
    else if (countDownDate > FajrTimeAF && countDownDate <= DhuhrTimeAF) {
        // Find the distance between now an the count down date
        var distance = DhuhrTimeAF - countDownDate;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        document.getElementById("ComingSoon").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
        //display = document.querySelector('#ComingSoon') + " UPCOMING PRAYER DHUHR";

    }
    else if (countDownDate > DhuhrTimeAF && countDownDate <= AsrTimeAF) {
        // Find the distance between now an the count down date
        var distance = AsrTimeAF - countDownDatenow;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        document.getElementById("ComingSoon").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
        //  display = document.querySelector('#ComingSoon') + "UPCOMING PRAYER ASR";

    }
    else if (countDownDate > AsrTimeAF && countDownDate <= MaghribTimeAF) {
        // Find the distance between now an the count down date
        // Find the distance between now an the count down date
        var distance = countDownDatenow - MaghribTimeAF;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        document.getElementById("ComingSoon").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
        //display = document.querySelector('#ComingSoon') + " UPCOMING PRAYER MAGHRIB";

    }
    else if (countDownDate > MaghribTimeAF && countDownDate <= IshaTimeAF) {
        // Find the distance between now an the count down date
        var distance = countDownDate - IshaTimeAF;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
        // Output the result in an element with id="demo"
        document.getElementById("ComingSoon").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
        //  display = document.querySelector('#ComingSoon') + " UPCOMING PRAYER ISHA";

    }


    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        // document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);

1 个答案:

答案 0 :(得分:0)

如果您很高兴使用第三方库来处理与日期相关的功能,那么moment.js可能就是您所需要的。

moment().fromNow();(从现在开始计算剩余时间到你的时间)和使用moment().isBefore(Moment|String|Number|Date|Array);的自定义排序功能的组合可能会做你需要的。

我没有对此进行过测试,但这样的事情应该有效:

var FajrTimeAF = moment({hour: 3, minute: 5})
var DhuhrAF = moment({hour: 13, minute: 10})
var nextTime = moment(FajrTimeAF).isBefore(DhuhrAF) ? FajrTimeAF : DhuhrAF // repeat this check as necessary for other important times
var timeLeft = moment().to(nextTime) // returns remaining time until nextTime