简体   繁体   English

将javascript Date()转换为UTC和偏移时间

[英]Convert javascript Date() to UTC and offset time

I have a countdown timer on a website (it acts as a same day shipment countdown timer, so the visitor knows if they place an order, it will be shipped out today if they're within the time window.) Basically the timer just counts down monday to friday until 5:00PM and then starts again at "0" (midnight, 24 hour clock) which was all working. 我在网站上有一个倒数计时器(它作为当天发货倒数计时器,所以访问者知道他们是否下了订单,如果他们在时间窗口内,它将在今天发货。)基本上计时器只计算星期一到星期五到下午5点,然后在“0”(午夜,24小时制)再次开始,这一切都正常。

Then I realized that since the time is client side (javascript) visitors on the PST timezone will see a false time compared to what they should see (the store is Eastern). 然后我意识到,由于时间是客户端(javascript)PST时区的访问者将看到他们应该看到的错误时间(商店是东部)。

Unfortunately I can't use php or anything server side to get the time from the server, so it has to be javascript (convert to UTC and offset). 不幸的是我不能使用PHP或任何服务器端从服务器获取时间,所以它必须是javascript(转换为UTC和偏移)。

I'm doing something wrong with the variables as far as I can tell, possibly more, could someone please tell me what I'm exactly setting wrong? 据我所知,我对变量做错了,可能更多,有人可以请告诉我我错误的设置是什么吗? (it doesn't show any errors in my console). (它在我的控制台中没有显示任何错误)。

if (document.getElementById('countdownTimer')) {
    pad = function(n, len) { // leading 0's
        var s = n.toString();
        return (new Array( (len - s.length + 1) ).join('0')) + s;
    };
    function calcTime(offset) {

        // create Date object for current location
       var d = new Date();

        // convert to msec
        // add local time zone offset 
        // get UTC time in msec
        utc = d.getTime() + (d.getTimezoneOffset() * 60000);
        offset = -5.0;  
        var now = utc + (3600000*offset);


    function countDown() {
       //var now = new Date();
        if ( (now.getDay() >= 1) && (now.getDay() <= 5) ) { // Monday to Friday only
            var target = 17; // 15:00hrs is the cut-off point
            if (now.getHours() < target) { // don't do anything if we're past the cut-off point
                var hrs = (target - 1) - now.getHours();
                if (hrs < 0) hrs = 0;
                var mins = 59 - now.getMinutes();
                if (mins < 0) mins = 0;
                var secs = 59 - now.getSeconds();
                if (secs < 0) secs = 0;
                var str = '<span id="day">00</span><span id="hour">' + pad(hrs, 2) + '</span><span id="minute">' + pad(mins, 2) + '</span><span id="second">' + pad(secs, 2) + '</span>';
                document.getElementById('countdownTimer').innerHTML = str;
            }
        }
    }
    var timerRunning = setInterval('countDown()', 1000);
}
}

I see that in these lines : 我在这些方面看到了:

    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    offset = -5.0;  
    var now = utc + (3600000*offset);

you're creating a now variable as a number, and then in your function countDown() you're using it as a date object. 你正在创建一个now变量作为数字,然后在你的函数countDown()你将它用作日期对象。 You should create your now var as a date like this 您应该将now var创建为这样的日期

var now = new Date(utc + (3600000*offset));

I just did this to set an expires header in node.js ... here's what I did: 我这样做是为了在node.js中设置一个expires头...这就是我做的:

res.setHeader("Expires", new Date(Date.now() + 345600000).toUTCString());

for another application you could do it differently: 对于另一个应用程序,您可以采用不同的

var updated_date = new Date(Date.now() + 345600000, //miliseconds
    utc_date = updated_date.toUTCString()

Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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