1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Javascript Date

Discussion in 'Web Design & Development' started by cpscdave, Apr 26, 2010.

Thread Status:
Not open for further replies.
  1. cpscdave

    cpscdave Thread Starter

    Joined:
    Feb 25, 2004
    Messages:
    444
    Hi everyone

    We have a little javascript function that gives a countdown
    Code:
    <script type="text/javascript">
    
    var now = new Date();
    var event = new Date("<?=$lookout_expiry?>");
    var time_left = (event - now) / 1000;
    var minutes = Math.floor(time_left / 60);
    var seconds = Math.floor(time_left % 60);
    
    
    ID=window.setTimeout("update();", 1000);
    function update() {
    now = new Date();
     time_left = (event - now) / 1000;
     minutes = Math.floor(time_left / 60);
     seconds = Math.floor(time_left % 60);
    
    //seconds = (event - now) / 1000;
    //seconds = Math.round(seconds);
    //minutes = seconds / 60;
    //minutes = Math.round(minutes);
    document.counter.minutes.value = minutes;
    document.counter.seconds.value = seconds;
    ID=window.setTimeout("update();",1000);
    }
    </script>
    
    
    The code worked great except on the live server which is 1 hour behind us in time, the countdown is off by 1 hour!
    Is there an easy fix for this?
     
  2. cpscdave

    cpscdave Thread Starter

    Joined:
    Feb 25, 2004
    Messages:
    444
    Found a work around.
    This works cause in this cause I always know how long the countdown needs to be

    Code:
    <script type="text/javascript">
    
    
    var time_left = 300; 
    var minutes = Math.floor(time_left / 60);
    var seconds = Math.floor(time_left % 60);
    
    
    ID=window.setTimeout("update();", 1000);
    
    function update() {
        if ( typeof update.counter == 'undefined' ) {
            // It has not... perform the initilization
            update.counter = 0;
        }
    
    update.counter+=1;
    
    
     time_left = 300 - update.counter;
     if(time_left <0){
       time_left = 0; }
     minutes = Math.floor(time_left / 60);
     seconds = Math.floor(time_left % 60);
    
    //seconds = (event - now) / 1000;
    //seconds = Math.round(seconds);
    //minutes = seconds / 60;
    //minutes = Math.round(minutes);
    document.counter.minutes.value = minutes;
    document.counter.seconds.value = seconds;
    ID=window.setTimeout("update();",1000);
    }
    </script>
    
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/919441

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice