This is my full code of month calender. It also show time on today’s date. The gettime function return the current time, but the time is static. To make the time dynamic what should I do?
<html>
<head>
<script type="text/javascript">
function gettime()
{
var time=new Date()
var hour=time.getHours()
var minute=time.getMinutes()
var text=hour+':'+ minute
return text
}
function nameofmonth(month)
{
var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
return monthname[month]
}
function monthdays(month,year)
{
var daysofmonth=new Array(31,28,31,31,30,31,31,31,30,31,30,31)
if(year%4==0)
daysofmonth[1]=29
return daysofmonth[month]
}
function table()
{
var now=new Date()
var hour=now.getHours()
var minute=now.getMinutes()
var second=now.getSeconds()
var date=now.getDate()
var month=now.getMonth()
var year=now.getFullYear()
now.setFullYear(year,month,1)
var firstday=now.getDay()
var monthname=nameofmonth(month)
var daysofmonth=monthdays(month,year)
if(firstday>0)
var k=-(firstday-1)
else
k=1
var table="<table border=5 cellspacing=9 cellpadding=13>"
table +="<th colspan=7>"+monthname+" "+date+"th</th>"
table +="<tr><th>sun</th><th>mon</th><th>tue</><th>wed</th><th>thu</th<th>fri</th><th>sat</th></tr>"
for(var i=0;i<5;i++)
{
table+="<tr>"
for(var j=0;j<7;j++)
{
if(k<=daysofmonth && k>0)
{ if(k==date)
table+='<td bgcolor="aqua">'+k+'<br>'+gettime()+ '</td>'
else
table+='<td>'+k+'</td>'
k=parseInt(k)
}
else
table+="<td></td>"
k++
}
table+="</tr>"
}
table+="</table>"
document.write(table)
}
</script>
</head>
<body onload="table()">
</body >
</html>
Add
<div id='clock'></div>to the document. Intable()replace document.write withWrite a separate onload function, in this function use
setIntervalto calltable()periodically.