JS控制日期顯示的小例子
我們在項(xiàng)目中一般會遇到顯示時(shí)間的問題,一般的處理方式是在前臺通過JS控制,JS控制顯示時(shí)間的代碼如下,各種不同的顯示方式:
function Clock() {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[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 "現(xiàn)在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
};//現(xiàn)在是<SPAN id=clock>現(xiàn)在是:2013年3月6日 13:54:17 星期三</SPAN>
<SPAN></SPAN>
this.toSimpleDate = function() {
return this.year + "-" + this.month + "-" + this.date;
};//2013-03-06
this.toDetailDate = function() {
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();//顯示方式調(diào)用
window.setTimeout(function() {clock.display(ele);}, 1000);
};
}
相關(guān)文章
layui監(jiān)聽下拉選框選中值變化的方法(包含監(jiān)聽普通下拉選框)
今天小編大家分享一篇layui監(jiān)聽下拉選框選中值變化的方法(包含監(jiān)聽普通下拉選框),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09javascript前端和后臺進(jìn)行數(shù)據(jù)交互方法示例
這篇文章主要介紹了javascript前端和后臺進(jìn)行數(shù)據(jù)交互方法示例,文章通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08javascript中callee與caller的用法和應(yīng)用場景
javascript中callee和caller的用法和應(yīng)用場景分析,需要的朋友可以參考下。2010-12-12JavaScript跨平臺的開源框架NativeScript
本文給大家分享的是一款使用javascript來構(gòu)建跨平臺原生移動應(yīng)用的開源框架--NativeScript,可以使用JavaScript開發(fā)跨平臺、真正原生的iOS, Android 和 Windows 移動App。開發(fā)人員使用NativeScript提供的庫來構(gòu)建應(yīng)用UI,其抽象了各種原生平臺之間的不同。2015-03-03使用javascript實(shí)現(xiàn)頁面定時(shí)跳轉(zhuǎn)總結(jié)篇
下面對使用JavaScript實(shí)現(xiàn)頁面定時(shí)跳轉(zhuǎn)做一下總結(jié),各種定時(shí)跳轉(zhuǎn)代碼記錄如下,希望對大家有所幫助2013-09-09JavaScript對JSON數(shù)組簡單排序操作示例
這篇文章主要介紹了JavaScript對JSON數(shù)組簡單排序操作,結(jié)合實(shí)例形式分析了javascript使用sort()方法針對json數(shù)組元素排序的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-01-01