0

I need for a clock to count from a specific time. e.g. Time is 20:08:00 and then to count from there. I have searched high and low for an answer and no one has specifically come up with an answer(that Ive seen). So my normal clock is like this.

<script type="text/javascript">
function clock() 
{
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
dispTime = hours + ":" + minutes + ":" + seconds;
var basicclock = document.getElementById('basicclock');
basicclock.innerHTML = dispTime;
setTimeout("clock()", 1000);
}
clock();
</script>

So all I need is the time to start at say 20:08:00 (or a variable of time). I am wondering if it better to use a timer to achieve a set time and to count from that???

Any help would be appreciated.

1 Answers1

0

First: Please try to extensively search SO for answers before asking questions, many helpful responses can be found if you look. ;)

If you are trying to countdown to a certain time/date I would recommend the answer found HERE

All code credit goes to author's answer above. HTML - for display

<span id="days"></span>
<span id="hours"></span>
<span id="minutes"></span>
<span id="seconds"></span>

Script (keep formatting and just modify the 4th line down for your target date)

setInterval(function(){
    // set whatever future date / time you want here, together with
    // your timezone setting...
    var future = new Date("Sep 20 2014 21:15:00 GMT+0200");
    var now = new Date();
    var difference = Math.floor((future - now) / 1000);

    var seconds = fixIntegers(difference % 60);
    difference = Math.floor(difference / 60);

    var minutes = fixIntegers(difference % 60);
    difference = Math.floor(difference / 60);

    var hours = fixIntegers(difference % 24);
    difference = Math.floor(difference / 24);

    var days = difference;

    $("#seconds").text(seconds + "s");
    $("#minutes").text(minutes + "m");
    $("#hours").text(hours + "h");
    $("#days").text(days + "d");
}, 1000);

function fixIntegers(integer)
{
    if (integer < 0)
        integer = 0;
    if (integer < 10)
        return "0" + integer;
    return "" + integer;
}

DEMO OF THE ABOVE CODE

I would also look at these are other interesting solutions found on this post here HERE

Community
  • 1
  • 1
Austin
  • 3,010
  • 23
  • 62
  • 97
  • Thank you for your quick reply.... I will look at all this and see if I can get it to work. i want the time to count forward, like a normal clock from a given time. Still I am confident with your supplied information I will be able to get this working. Many thanks. – user3756433 Jun 19 '14 at 20:21
  • @user3756433 I can help later if you are still having issues, just let me know! – Austin Jun 19 '14 at 21:02
  • I am still having some issues. I have a Time (e.g.21:10:00) and for the clock to start from that time and count forward. Ive tried your method but I think it should be easier to work with a timer. 1. Set Timer 2. Count Forward (hr,min,sec) and 3. Display the time. – user3756433 Jun 20 '14 at 10:53