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


Source Code
loading clock preview...

Comments

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

You can add style tags to the div tag in the last line:
<div id="clockbox" style="text-align: center;"></div>

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

d = new Date();
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: d = new Date();with:
d = new Date();
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">
offset = Math.round(new Date().getTime() / 1000);

and replace
d = new Date();with:
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");

Hi - Thanks so much for this code - it is hugely useful. Small question, do you know what I add to make the date text bold please? Sorry I am a noob to javascript!

Thanks,
AB

you can edit the last line of your generated code and add a style="" property like:
<div id="clockbox" style="font-weight:bold;"></div>

Hello...thank you for this amazing tool...it is extremely helpful...I am trying to put the date (ex Wednesday, November 12th, 2013 ) at the top of my web page and the time (ex 9:00 AM ) at the bottom of the page...However when I add both scripts one script seems to override the other...I have changed the function names and the id names also...below is the scripts...

<script type="text/javascript">
    tday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    tmonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    function GetClock() {
        d = new Date();
        nday = d.getDay();
        nmonth = d.getMonth();
        ndate = d.getDate();
        nyear = d.getYear();
        if (nyear < 1000) nyear = nyear + 1900;


        document.getElementById('clockbox').innerHTML = "" + tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + "";
        setTimeout("GetClock()", 1000);
    }
    window.onload = GetClock;
</script>

and

<script type="text/javascript">

    function GetTime() {
        c = new Date();
        zday = c.getDay();
        zmonth = c.getMonth();
        zdate = c.getDate();
        zyear = c.getYear();
        zhour = c.getHours();
        zmin = c.getMinutes();
        if (zyear < 1000) zyear = zyear + 1900;

        if (zhour == 0) { zp = " AM"; zhour = 12; }
        else if (zhour <= 11) { zp = " AM"; }
        else if (zhour == 12) { zp = " PM"; }
        else if (zhour >= 13) { zp = " PM"; zhour -= 12; }

        if (zmin <= 9) { zmin = "0" + zmin; }

        document.getElementById('timebox').innerHTML = "" + (zmonth + 1) + "/" + zdate + "/" + zyear + " " + zhour + ":" + zmin + zp + "";
        setTimeout("GetTime()", 1000);
    }
    window.onload = GetTime;
</script>

The div ids that are calling are below:

<center><div id="clockbox" class="date_holder_splash"></div></center>
<div id="timebox" class="time_holder_splash"></div>

any help would be appreciated...thank ya...and Happy Thanksgiving...

You did a really good job. You just need to combine the onload events because they are overwriting each other. so delete
window.onload = GetClock;
window.onload = GetTime;and use a single

window.onload = function(){
GetTime();
GetClock();
}

Hey love the code, it's perfect but is there a way to use more than one on the same page? I want to show LA & NYC time, but the codes are interfering with each other? I read here that someone had a similar problem but I do not understand the solution, thank you.

This script really wasn't setup too support multiple copies on the same page. If you paste the code for your two countdowns, I'll go ahead and change one for you so they'll both work.

Hi very useful clock, but how can I change Date from 12Hr to 24Hr format.

Thanks

There are already several 24-hour options you can choose from. However, if you want to change your existing clock change

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

Sir,
Many Many thanks for this java scripts. i was searching for this since many months.
I shall be using this for my personal use.
Thanks

Would it be possible to adjust the code so that the clock showed sidereal (astronomical) time - where 24 sidereal hours equals 23hrs56m4.1s? This would mean, in effect that the clock would have to run about 1.0027378% faster....

I've never used sidereal time before, but after a quick search I believe it should be possible to have an approximate sidereal clock. However, figuring out the math and then making it function in Javascript is more work than I want to undertake for a free project.

I'd suggest searching for a web based sidereal clock and see if you can find one someone else already wrote. If no luck, it is something I might be interested in as a paid project.