Home >Web Front-end >JS Tutorial >Use js to display the current time example_jquery
The page displays in the foreground
js script
//The first method of displaying the current system time
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);
}
//The second method of displaying the current system time
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 "The current time is:" this.year "Year" 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);
};
}