jameswildi
jameswildi

Reputation: 31

How to make Javascript time automatically update

I am using the following Javascript code to display the time on my website. How can I make this update automatically.

Thanks

<section class="portlet grid_6 leading"> 
<header>
<h2>Time<span id="time_span"></span></h2>
</header>
<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
    minutes = "0" + minutes
}
var t_str = hours + ":" + minutes + " ";
if(hours > 11){
    t_str += "PM";
} else {
   t_str += "AM";
}
document.getElementById('time_span').innerHTML = t_str;
</script>
</section>

Upvotes: 3

Views: 60820

Answers (9)

Rajesh
Rajesh

Reputation: 1

GetTime();

function GetTime(){
  var CurrentTime = new Date()
  var hour = CurrentTime.getHours()
  var minute = CurrentTime.getMinutes()
  var second = CurrentTime.getSeconds()

  if(minute < 10){
    minute = "0" + minute
  }

  if(second < 10){
    second = "0" + second
  }

  var GetCurrentTime = hour + ":" + minute + ":" + second + " ";

  if(hour > 11){
    GetCurrentTime += "p.m."
  }else{
    GetCurrentTime += "a.m."
  }
<!-- Try changing innerHTML to document.getElementById("CurrentTime").value -->
  document.getElementById("CurrentTime").value = GetCurrentTime;
  setTimeout(GetTime,1000)
}
<span id="CurrentTime"></span>

Upvotes: 0

Name
Name

Reputation: 11

GetTime();

function GetTime(){
  var CurrentTime = new Date()
  var hour = CurrentTime.getHours()
  var minute = CurrentTime.getMinutes()
  var second = CurrentTime.getSeconds()

  if(minute < 10){
    minute = "0" + minute
  }

  if(second < 10){
    second = "0" + second
  }

  var GetCurrentTime = hour + ":" + minute + ":" + second + " ";

  if(hour > 11){
    GetCurrentTime += "p.m."
  }else{
    GetCurrentTime += "a.m."
  }

  document.getElementById("CurrentTime").innerHTML = GetCurrentTime;
  setTimeout(GetTime,1000)
}
<span id="CurrentTime"></span>

Upvotes: 1

Satya Chandu
Satya Chandu

Reputation: 1

This code output format->00:00:00 and refresh automatically like real time clock, hope it works..

function r(txt) {
    document.write(tex);
}

function createTIME() {
    d = new Date();
    var time = addZERO(d.getHours()) + ':' + addZERO(d.getMinutes()) + ':' + addZERO(d.getSeconds());
    return 'Present Time = ' + time;
}

function doDyn() {
    document.getElementById('Dyn').innerHTML = createTIME();
}

function addZERO(val) {
    return ((val < 10) ? '0' : '') + val;
}

Upvotes: 0

Amornbordee
Amornbordee

Reputation: 1

timer();

function timer(){
 var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var sec = currentTime.getSeconds()
if (minutes < 10){
    minutes = "0" + minutes
}
if (sec < 10){
    sec = "0" + sec
}
var t_str = hours + ":" + minutes + ":" + sec + " ";
if(hours > 11){
    t_str += "PM";
} else {
   t_str += "AM";
}
 document.getElementById('time_span').innerHTML = t_str;
 setTimeout(timer,1000);
}
<header>
<h2>Time<span id="time_span"></span></h2>
</header>

Upvotes: -1

KooiInc
KooiInc

Reputation: 122898

A bit less messy would be:

timer();

function timer(){
 var now     = new Date,
     hours   = now.getHours(),
     ampm    = hours<12 ? ' AM' : ' PM',
     minutes = now.getMinutes(),
     seconds = now.getSeconds(),
     t_str   = [hours-12, //otherwise: what's the use of AM/PM?
                (minutes < 10 ? "0" + minutes : minutes),
                (seconds < 10 ? "0" + seconds : seconds)]
                 .join(':') + ampm;
 document.getElementById('time_span').innerHTML = t_str;
 setTimeout(timer,1000);
}

The timer updates (roughly) every second (= 1000 Ms), using setTimeout from within the timer function.

​See it in action

Upvotes: 0

Alexis Pigeon
Alexis Pigeon

Reputation: 7512

Add all your javascript code in a function called updateClock() placed in the <head> section of your page, and alter the <body> tag that way:

<body onload="updateClock(); setInterval('updateClock()', 1000 )">

It will recalculate and redisplay the time every second. Since you only display hours and minutes, you can use a longer interval. If you want to update time every numSeconds you should use something like

<body onload="updateClock(); setInterval('updateClock()', numSeconds * 1000 )">

And of course, this one is just one of many gazillions solutions that you can find out there.

Upvotes: 3

Parth Thakkar
Parth Thakkar

Reputation: 5475

try this, a tidier version:

var el = document.getElementById('time_span')
setInterval(function() {

    var currentTime = new Date(),
        hours = currentTime.getHours(),
        minutes = currentTime.getMinutes(),
        ampm = hours > 11 ? 'PM' : 'AM';

    hours += hours < 10 ? '0' : '';
    minutes += minutes < 10 ? '0' : '';

    el.innerHTML = hours + ":" + minutes + " " + ampm;
}, 1000);

Upvotes: 1

Recognizer
Recognizer

Reputation: 756

There are plenty of clock libraries out there. Perhaps check out this previous post: How to create a jquery clock timer

Upvotes: 1

UltraInstinct
UltraInstinct

Reputation: 44434

Use setTimeout(..) to call a function after a specific time. In this specific case, it is better to use setInterval(..)


function updateTime(){
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    if (minutes < 10){
        minutes = "0" + minutes
    }
    var t_str = hours + ":" + minutes + " ";
    if(hours > 11){
        t_str += "PM";
    } else {
        t_str += "AM";
    }
    document.getElementById('time_span').innerHTML = t_str;
}
setInterval(updateTime, 1000);

Upvotes: 13

Related Questions