JavaScript编程开发使用js显示当前时间示例
页面前台显示
. 代码如下:
<span id="clock" style="font-size:14px;"></span>
js脚本
. 代码如下:
$(document).ready(function () {
//第一种
showtime();
//第二种
var clock = new clock();
clock.display($("#clock"));
});
//显示当前时间处理 第一种方法
function showtime() {
var myarray = new array(7);
var td = new date();
myarray[0] = "星期日";
myarray[1] = "星期一";
myarray[2] = "星期二";
myarray[3] = "星期三";
myarray[4] = "星期四";
myarray[5] = "星期五";
myarray[6] = "星期六";
weekday = td.getday();
var h = td.gethours();
var m = td.getminutes();
var s = td.getseconds();
var hstr = h;
var mstr = m;
var istr = s;
if (h < 10) { hstr = "0" + h };
if (m < 10) { mstr = "0" + m };
if (s < 10) { istr = "0" + s };
$("#clock").innerhtml('当前时间:' + new date().tolocaledatestring() + " " + myarray[weekday] + " " + hstr + ":" + mstr + ":" + istr);
settimeout(showtime, 1000);
}
//显示系统当前时间处理 第二种方法
function clock() {
var date = new date();
this.year=date.getfullyear();
this.month=date.getmonth()+1;
this.date=date.getdate();
this.day=newarray("星期日","星期一","星期二","星期三","星期四","星期五","星期六")[date.getday()];
this.hour=date.gethours()<10?"0"+date.gethours():date.gethours();
this.minute=date.getminutes()<10?"0"+date.getminutes():date.getminutes();
this.second=date.getseconds()<10?"0"+date.getseconds():date.getseconds();
this.tostring=function(){
return"现在时间是:"+this.year+"年"+this.month+"月"+this.date+"日"+this.hour+":"+this.minute+":"+this.second+""+this.day;
};
this.tosimpledate=function(){
returnthis.year+"-"+this.month+"-"+this.date;
};
this.todetaildate=function(){
returnthis.year+"-"+this.month+"-"+this.date+""+this.hour+":"+this.minute+":"+this.second;
};
this.display=function(ele){
varclock=newclock();
ele.innerhtml=clock.tostring();
window.settimeout(function(){clock.display(ele);},1000);
};
}
上一篇: 关于jQuery中fade(),show()起始位置的一点小发现
下一篇: 天天送快递