Javascript 显示时间

来源:互联网 发布:淘宝网开衫毛衣 编辑:程序博客网 时间:2024/06/06 06:48

简述:

用javascript基本的Date函数书写一个时间输出的函数


知识点:

1)用到了点OO的思想,以调用MainFunc的getDate方法得到一个时间的字符串

2)用到了Date()类


代码:

<!DOCTYPE html><html><head><B>Show Time: </B><br><br><script type = "text/javascript">  var MainFunc = function(){    this.getDate = function(){      var now = new Date();      //document.write(now.toLocaleString());      var year = now.getFullYear();      var month = 1 + now.getMonth();      var day = now.getDate();      var hour = now.getHours();      var minute = now.getMinutes();      var second = now.getSeconds();      var str = "year: " + year + " month: " + month + " day: " + day+ " hour: " + hour + " minute: " + minute + " second: " + second;  return str;}  };  var showDate = new MainFunc();  document.write(showDate.getDate());</script></head><body></body></html>

输出:

Show Time:

year: 2012 month: 7 day: 9 hour: 10 minute: 49 second: 35