js 显示当前日期
复制代码 代码如下:
效果如下图:
相应的js如下:
复制代码 代码如下:
//创建一个数组,用于存放每个月的天数
function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11) {
this[0] = m0;
this[1] = m1;
this[2] = m2;
this[3] = m3;
this[4] = m4;
this[5] = m5;
this[6] = m6;
this[7] = m7;
this[8] = m8;
this[9] = m9;
this[10] = m10;
this[11] = m11;
}
//实现月历
function calendar() {
var monthnames = janfebmaraprmayjunjulaugsepoctnovdec;
var today = new date();
var thisday;
var monthdays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
year = today.getyear() +1900;
thisday = today.getdate();
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
monthdays[1] = 29;
ndays = monthdays[today.getmonth()];
firstday = today;
firstday.setdate(1);
testme = firstday.getdate();
if (testme == 2)
firstday.setdate(0);
startday = firstday.getday();
var daynames = new array(星期日,星期一,星期二,星期三,星期四,星期五,星期六);
var monthnames = new array(1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月);
var now = new date();
document.write()
document.write()
document.write();
document.write( );
document.write( );
document.write( );
document.writeln( + 公元 + now.getyear() + 年 + monthnames[now.getmonth()] + + now.getdate() + 日 + daynames[now.getday()] + );
document.writeln(
日);
document.writeln( 一);
document.writeln( 二);
document.writeln( 三);
document.writeln( 四);
document.writeln( 五);
document.writeln( 六);
document.writeln(
);
column = 0;
for (i=0; idocument.writeln(\n );
column++;
}
for (i=1; iif (i == thisday) {
document.writeln()
}
else {
document.writeln( );
}
document.writeln(i);
if (i == thisday)
document.writeln( )
column++;
if (column == 7) {
document.writeln();
column = 0;
}
}
document.writeln(
)
document.writeln()
document.writeln(现在时间:
)
document.writeln(
);
}
var timerid = null;
var timerrunning = false;
function stopclock (){
if(timerrunning)
cleartimeout(timerid);
timerrunning = false;
}
//显示当前时间
function showtime () {
var now = new date();
var hours = now.gethours();
var minutes = now.getminutes();
var seconds = now.getseconds()
var timevalue = + ((hours >12) ? hours -12 :hours)
timevalue += ((minutes timevalue += ((seconds timevalue += (hours >= 12) ? 下午 : 上午
document.clock.face.value = timevalue;
timerid = settimeout(showtime(),1000);//设置超时,使时间动态显示
timerrunning = true;
}
function startclock () {
stopclock();
showtime();
}
使用页面显示如下:
复制代码 代码如下:
这就ok了!