Jon Girard
Jon Girard

Reputation: 247

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.

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).

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).

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

Upvotes: 0

Views: 7961

Answers (2)

Brian Noah
Brian Noah

Reputation: 2972

I just did this to set an expires header in node.js ... here's what I did:

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!

Upvotes: 0

Leo
Leo

Reputation: 1919

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. You should create your now var as a date like this

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

Upvotes: 2

Related Questions