我构造了一个倒数计时器,但是它只是使用getTime(),我不确定如何调整它,所以它是我想要的正确时区(PDT / PT)

var countdownTimer = setInterval(countdownTick, 1000);

function countdownTick() {
    jQuery('ul.countdown').each(function() {
        var date = jQuery(this).attr('data-date').split('-'); // Create date array from attribute
        var time = jQuery(this).attr('data-time').split('-'); // Create time array from attribute
        for (var i = 0; i < date.length; i++) {
            date[i] = parseInt(date[i]);
        }
        for (var i = 0; i < time.length; i++) {
            time[i] = parseInt(time[i]);
        }
        var today = new Date();
        var theDate = new Date(date[0], (date[1] - 1), date[2], time[0], time[1]);
        if (theDate.getTime() > today.getTime()) { // If the target date is in the future
            countdownCalc(this, theDate, today); // Calculate how much time there is until the target date
        }
    });
}

function countdownCalc(obj, targetDate, currentDate) {
    var oneDay = 1000 * 60 * 60 * 24;
    var oneSecond = 1000;
    var output = (targetDate.getTime() - currentDate.getTime());
    var day = Math.floor(output / oneDay);
    var hour = Math.floor((output - (day * oneDay)) / (1000 * 60 * 60));
    var minute = Math.floor((output - (hour * (1000 * 60 * 60) + (day * oneDay))) / (1000 * 60));
    var second = Math.floor((output - ((minute * 60000) + (hour * 1000 * 60 * 60) + (day * oneDay))) / 1000);
    jQuery(obj).html('<li><span class="countdown-label">DAYS</span><span class="countdown-number">' + day + '</span></li><li><span class="countdown-label">HOURS</span><span class="countdown-number">' + hour + '</span></li><li><span class="countdown-label">MINUTES</span><span class="countdown-number">' + minute + '</span></li><li><span class="countdown-label">SECONDS</span><span class="countdown-number">' + second + '</span></li>');
}


https://jsfiddle.net/4nag4h5v/

最佳答案

这是使用moment-timezone.js插件变得有用的地方

我们能够做的事情很简单,例如:

let time = Date.now();
moment(time).tz("YOURTIMEZONE").format('x') // get timestamp (in milliseconds

关于javascript - 如何在JavaScript中获取UTC时区并调整为PDT/PT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47145706/

10-10 06:57