the Forum

How to add the current time using Javascript.

Posted on March 27, 2012 by Michael Rosario
Total Posts: 33  |  Join date: 03-12-11

This is a simple code to add a date on your website. You put the following code within the HEAD tags:
<script type="text/javascript" language="JavaScript">

// WE SETUP THE FUNCTIONS
function getCurrentDate()
{
var months = new Array(13);
    
months[0] "January";
    
months[1] "February";
    
months[2] "March";
    
months[3] "April";
    
months[4] "May";
    
months[5] "June";
    
months[6] "July";
    
months[7] "August";
    
months[8] "September";
    
months[9] "October";
    
months[10] "November";
    
months[11] "December";
    var 
now = new Date();

    var 
mnum now.getMonth();
    var 
months[mnum];
    var 
now.getDate();
    var 
now.getYear();
    if(
2000{ y 1900}
    
var dateString ' ' +  ', ' +  y;

return 
dateString;
// Used to get the current Date

function getCurrentTime()
{
    
var now = new Date();
    var 
now.getHours();
    var 
now.getMinutes();
    var 
now.getSeconds();
    var 
dn "AM";
    if (
11{ dn "PM"}
    
if (12{ h hour 12}
    
if (== 0{ h 12}
    
if (10{ h "0" hour}
    
if (10{ m "0" minute}
    
if (10{ s "0" second}
    
var timeString ':' ':' " " dn;
    return 
timeString;
// Used to get the current Time

//-->
</script> 
Then you put the following anywhere on your site below the BODY tag:
<script type="text/javascript" language="JavaScript">
<!--
    
// This is to display the time and date on your site.
    
var calendarDate getCurrentDate();
    var 
clockTime getCurrentTime();
    
document.write('The date is' calendarDate '<br />');
    
document.write('The current time is ' clockTime);
//-->
</script> 

Tags: There are no tags for this entry.

There are no answers yet.  Add yours below.

add your answers here
comments powered by Disqus