Clock.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. function Clock() {
  2. var date = new Date();
  3. this.year = date.getFullYear();
  4. this.month = date.getMonth() + 1;
  5. this.date = date.getDate();
  6. this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
  7. this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  8. this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  9. this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  10. this.toString = function() {
  11. return "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
  12. };
  13. this.toSimpleDate = function() {
  14. return this.year + "-" + this.month + "-" + this.date;
  15. };
  16. this.toDetailDate = function() {
  17. return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;
  18. };
  19. this.display = function(ele) {
  20. var clock = new Clock();
  21. ele.innerHTML = clock.toString();
  22. window.setTimeout(function() {clock.display(ele);}, 1000);
  23. };
  24. }