Show the current time example using js

  • 2020-03-30 02:11:31
  • OfStack

Page foreground display


<span id="clock" style="font-size:14px;"></span>

Js script


$(document).ready(function () {
//The first kind of
showTime();
//The second,
var clock = new Clock();
clock.display($("#clock"));
});
// Displays system current time processing  The first kind of methods  
function showTime() {
 var myArray = new Array(7);
 var TD = new Date();
 myArray[0] = " Sunday ";
 myArray[1] = " Monday ";
 myArray[2] = " Tuesday ";
 myArray[3] = " Wednesday ";
 myArray[4] = " Thursday ";
 myArray[5] = " Friday ";
 myArray[6] = " Saturday ";
 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(' Current time: ' + new Date().toLocaleDateString() + " " + myArray[weekday] + " " + hstr + ":" + mstr + ":" + istr);
 setTimeout(showTime, 1000);
}
// Displays system current time processing  The second, methods 
function Clock() {
var date = new Date();
this.year=date.getFullYear();
this.month=date.getMonth()+1;
this.date=date.getDate();
this.day=newArray(" Sunday "," Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday ")[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" Now the time is :"+this.year+" years "+this.month+" month "+this.date+" day "+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);
};
}


Related articles: