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);
}
本文介绍了一个使用JavaScript编写的函数,该函数能够实时地在网页上显示当前的时间和日期。通过对浏览器内置Date对象的方法进行调用,实现了获取并格式化显示当前的小时、分钟、秒、年、月、日。
869

被折叠的 条评论
为什么被折叠?



