Home  >  Article  >  Web Front-end  >  Use js to display the current time example_jquery

Use js to display the current time example_jquery

WBOY
WBOYOriginal
2016-05-16 16:57:421084browse

The page displays in the foreground

Copy code The code is as follows:


js script

Copy code The code is as follows:

$(document).ready( function () {
//The first type
showTime();
//The second type
var clock = new Clock();
clock.display($("#clock "));
});

//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);
};
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn