now=new Date();
hours = now.getHours();? //得到小時
minutes = now.getMinutes();? //得到分鐘
seconds = now.getSeconds();? //得到秒
year=now.getYear();? //得到年份
month=now.getMonth()+1;? //得到月份
date=now.getDate();? //得到日期
day=now.getDay();? //得到星期數
一個顯示時間日期的函數:
function clock()
{
?var timeStr, dateStr;
?now = new Date();
?// time
?hours = now.getHours();
?minutes = now.getMinutes();
?seconds = now.getSeconds();
?if(hours < 10)
??timeStr = "0"+hours;
?else timeStr=hours;
?if(minutes < 10)
??timeStr+=":0"+minutes;
?else timeStr+=":"+minutes;
?if(seconds < 10)
??timeStr+=":0"+seconds;
?else timeStr+=":"+seconds;
?document.clock.time.value=timeStr;
?//date
?year=now.getYear();
?month=now.getMonth()+1;
?date=now.getDate();
?dateStr=year;
?if(month<10)
??dateStr+="/0"+month;
?else dateStr+="/"+month;
?if(date<10)
??dateStr+="/0"+date;
?else dateStr+="/"+date;
?document.clock.date.value=dateStr;
?Timer=setTimeout("clock()",1000);
}