我們在專案中通常會遇到顯示時間的問題,一般的處理方式是在前台透過JS控制,JS控制顯示時間的程式碼如下,各種不同的顯示方式:
function Clock() {
function Clock() {
>
Date();
this.year = date.getFullYear();
this.month = date.getMonth() 1;
this.date = date.getDate(); thisday = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date. getHours() this.minute = date.getMinutes() this.second = date.getSeconds()
this.toString = function() { return "現在是:" this.year "年" this.month "月" this.date "日" this.hour ":" this.minute ":" this.second " " this.day;
};//現在是現在是:2013年3月6日13:54:17 星期三
複製碼
代碼如下:
return this.year "-" this.month "-" this.date;
};//2013-03-06
this.toDetailDate = function(this. ) {
return this.year "-" this.month "-" this.date " " this.hour ":" this.minute ":" this.second;
};//2013-03- 06 13:45:43
this.display = function(ele) {
var clock = new Clock();
ele.innerHTML = clock.toString();///toString();//顯示方式呼叫 window.setTimeout(function() {clock.display(ele);}, 1000); };}