0

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);
}
}
5
  • most servers emit a Date header that an ajax HEAD request can fetch. you can also subtract their offset from 5*3600000 to get the difference between their time and E.S.T. Commented Jan 30, 2014 at 4:56
  • @dandavis this is a big commerce hosted site, so I'm not sure how they have it configured. I know their API doesn't return the stores timezone.. Commented Jan 30, 2014 at 4:58
  • you can go to the page the javascript runs on in chrome, press F11, then F5, click on the Network tab, click the first entry line, and then click headers to see if Date is among them. if yes, then google "ajax head headers". Commented Jan 30, 2014 at 5:00
  • @dandavis yeah in the headers the date is listed as GMT so I guess I can convert from that then Commented Jan 30, 2014 at 5:07
  • new Date(theServerTimeString) should give your server's time in the user's timezone, not needing any conversion... Commented Jan 30, 2014 at 5:12

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

2 Comments

this ended up being the solution I used and got working, more or less.
I think offset should possibly be something like -(d.getTimezoneOffset() / 60) instead of defining -5.0 as this does not take DST into consideration... but let me know - I'm not sure how "universal" this would be.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.