Home >Web Front-end >JS Tutorial >A small example of JS controlling date display_javascript skills

A small example of JS controlling date display_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:12:51966browse

We generally encounter the problem of displaying time in projects. The general way to deal with it is to control it through JS at the front desk. The code for JS to control the display time is as follows, with various display methods:

Copy code The code is as follows:

function Clock() {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() 1;
this.date = date.getDate();
this.day = new Array("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 is:" this.year "Year" this.month "Month" this.date "Day" this.hour ":" this.minute ":" this.second " " this.day;
};//Now is Now is: Wednesday, March 6, 2013 13:54:17

Copy code The code is as follows:


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();//Display Method call
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