0

I've got this code to show a countdown message between Monday - Wednesday until Wednesday noon, then it disappears. This is the code:

    <script>
    var today = new Date(),
        curDay = today.getDay();
    var deadline = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate() + 3 - curDay, 10, 59, 59));
    function time_remaining(endtime){
        var t = endtime - new Date();
        var seconds = Math.floor( (t/1000) % 60 );
        var minutes = Math.floor( (t/1000/60) % 60 );
        var hours = Math.floor( (t/(1000*60*60)) % 24 );
        var days = Math.floor(t / (1000 * 60 * 60 * 24));
        return {'total':t, 'days': days, 'hours':hours, 'minutes':minutes, 'seconds':seconds};
    }
    function run_clock(id,endtime){
        var clock = document.getElementById(id);
        if (null === clock) {
            return;
        }
        var days_span = clock.querySelector('.days');
        var hours_span = clock.querySelector('.hours');
        var minutes_span = clock.querySelector('.minutes');
        var seconds_span = clock.querySelector('.seconds');

        function update_clock(){
            var t = time_remaining(endtime);
            days_span.innerHTML = ('0' + t.days).slice(-2);
            hours_span.innerHTML = ('0' + t.hours).slice(-2);
            minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
            seconds_span.innerHTML = ('0' + t.seconds).slice(-2);
            day = today.getDay();
            if((t.total<=0) || (day === 0) || (day === 6) || (day === 5) || (day === 4)){
                clearInterval(timeinterval);
                document.getElementById('deadline_Container').style.display = "none";
            }
        }
        update_clock();
        var timeinterval = setInterval(update_clock, 1000);
    }
    if (curDay > 0 && today < deadline) {
        run_clock('clockdiv', deadline);
    }
</script>

Now this works fine, however if somebody is in a different time zone for example the timings of the countdown won't be right. So I was wondering if there is any way to run this off the server time rather than the local time of the machine the user is using?

MariaL
  • 1,112
  • 2
  • 16
  • 41
  • You can't use javascript to get server side time, because it runs in client machine, you will have to use AJAX to ask current datetime to the server. However, I'm sure you could just use some functions or libraries that can convert à datetime to the same datetime than your server (like CultureInfo can do in a C# server) – alexay68 Oct 17 '17 at 09:22
  • Possible duplicate of [How to detect user's timezone?](https://stackoverflow.com/questions/16525617/how-to-detect-users-timezone) – VTodorov Oct 17 '17 at 09:22
  • Yeah, get the client time with timezone, and create a server request that handles the initial difference so you can have a more reliable start of your countdown clock – Icepickle Oct 17 '17 at 09:23

0 Answers0