Website HTML Javascript Clock (Date and Time)

Description

  • This generator allows you to create the Javascript code necessary to put a text clock on your website that can display the current time (and date). The clock keeps updating in real time. Multiple time and date formats to choose from.

Steps

  1. Select how you want your clock/calendar to look
  2. Generate the HTML and JavaScript code for your clock (button at bottom)
  3. Preview your clock at the bottom above the code
  4. Copy and Paste the Source Code into your HTML page

Note

  • If you want to change the size, font, or color you can edit the last line of your generated code and add a style="" property like: <div id="clockbox" style="font:14pt Arial; color:#FF0000;"></div>


Text Clock Format
Date Format: Time Format:


Source Code
loading clock preview...

Comments

Thank you very much for this code. Only one question. How to put this code in center.

If you want to have the time use a specific timezone, replace: var d=new Date();with:
var tzOffset = -5;//set this to the number of hours offset from UTC

var d=new Date();
var dx=d.toGMTString();
dx=dx.substr(0,dx.length -3);
d.setTime(Date.parse(dx))
d.setHours(d.getHours()+tzOffset);
Setting the tzOffset value to the UTC hours offset your timezone has. If you don't know what value to use, when you go to set your clock in Windows, the timezones listed all show their UTC offset. Like (UTC-05:00) Eastern Time which would be -5.

NOTE: Daylight Savings Time is not automatically taken into account. Meaning Eastern Time is normally tzOffset = -5; but Eastern Time on DST is actually tzOffset = -4; value. Sadly there is no good way to account for DST using only JavaScript.

To get around the Daylight Savings Time bug above, you can use PHP to use the server to calculate the timezone offset and automatically adjust for DST if necessary. However, this does require that the server/page supports PHP (if you aren't sure, it probably does not support PHP).

If you have PHP, replace: var d=new Date();with:
var d=new Date();
var dx=d.toGMTString();
dx=dx.substr(0,dx.length -3);
d.setTime(Date.parse(dx))
d.setSeconds(d.getSeconds() + <?php date_default_timezone_set('America/New_York'); echo date('Z'); ?>);
where 'America/New_York' is the timezone you want to use.
List of Supported Timezones

You need to use PHP/Javascript to figure out the time difference between the server and the browser then adjust the new Date() accordingly.

For anyone wanting to do this in PHP, replace:
<script type="text/javascript">with:

<script type="text/javascript">
var offset = Math.round(new Date().getTime() / 1000);

and replace
var d=new Date();with:
var d=new Date();
d.setSeconds(d.getSeconds() + <?php echo time(); ?> - offset);

tday=new Array("Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag");
tmonth=new Array("Jänner","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");

To display the same clock in multiple locations, look for the line similar to: document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+ap+""; and change it to
var obj=document.querySelectorAll('.clockbox');
for(var i in obj){
obj[i].innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+ap+"";
}
note that the last part after .innerHTML= may be different in your code and you will need to keep your differences to retain your clock format choices (my example was using default formatting).

Then use <div class="clockbox"></div> anywhere on the page you want the clock to appear (note that it changed from id="" to class="" attribute)

GetClock.js

// JavaScript Document
function GetClock(id,os){
var tzOffset = os;

var d=new Date();
var dx=d.toGMTString();
dx=dx.substr(0,dx.length -3);
d.setTime(Date.parse(dx))
d.setHours(d.getHours()+tzOffset);

var nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;

if(nhour==0){ap=" AM";nhour=12;}
else if(nhour<12){ap=" AM";}
else if(nhour==12){ap=" PM";}
else if(nhour>12){ap=" PM";nhour-=12;}

if(nmin<=9) nmin="0"+nmin;
if(nsec<=9) nsec="0"+nsec;

document.getElementById(id).innerHTML=""+nhour+":"+nmin+":"+nsec+ap+"";
        setTimeout('GetClock("'+id+'",'+os+');','1000');
        return true;

}

Code to use in HTML

<?php
$DST
= date("I",time());

if (
$DST == 1) {echo '<td><div id="PST"></div><script type="text/javascript">window.onload = GetClock("PDT",-7);</script></td>
    <td><div id="MST"></div><script type="text/javascript">window.onload = GetClock("MDT",-6);</script></td>
    <td><div id="CST"></div><script type="text/javascript">window.onload = GetClock("CDT",-5);</script></td>
    <td><div id="EST"></div><script type="text/javascript">window.onload = GetClock("EDT",-4);</script></td>'
;} else {echo '<td><div id="PST"></div><script type="text/javascript">window.onload = GetClock("PST",-8);</script></td>
    <td><div id="MST"></div><script type="text/javascript">window.onload = GetClock("MST",-7);</script></td>
    <td><div id="CST"></div><script type="text/javascript">window.onload = GetClock("CST",-6);</script></td>
    <td><div id="EST"></div><script type="text/javascript">window.onload = GetClock("EST",-5);</script></td>'
;}
?>

This may not the the easiest or prettiest way to do this, but it does work.

Is the any way you could add st, nd, rd, th after the date. i.e. 1st and in Superscript

You can add the following block into any GetClock that has a date. Add it under any other if() checks:

     if(ndate>10&&ndate<20){ndate+="th";}
else if(ndate%10==1){ndate+="st";}
else if(ndate%10==2){ndate+="nd";}
else if(ndate%10==3){ndate+="rd";}
else{ndate+="th";}
with superscript, it'll look like
     if(ndate>10&&ndate<20){ndate+="<sup>th</sup>";}
else if(ndate%10==1){ndate+="<sup>st</sup>";}
else if(ndate%10==2){ndate+="<sup>nd</sup>";}
else if(ndate%10==3){ndate+="<sup>rd</sup>";}
else{ndate+="<sup>th</sup>";}

Can clock code be created that will display 1/10 & 1/100th seconds:

23:59:59.99

d.getMilliseconds() will display the value you want. So in the innerHTML="" line, add d.getMilliseconds() like:
document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+":"+nsec+"."+d.getMilliseconds()+"";

You will also need to change your clock to update more often than the default: once every 1000 milliseconds (once per second). Change the update interval to something like 10 milliseconds: setInterval(GetClock,10);

How do you display the date and time with the break as such:

4:07PM
Sunday,December 2016

I've tried having multiple versions of the script running but the second one always overwrites the first.
Hope you can help!

Near the bottom you you'll have a line that controls the formatting when it prints to the page. By default it might look like:
document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+ap+"";but you can rearrange the order of the elements or add a line break like
document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+ap+"<br/>"+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+""; (that should be close to the format you asked for)