首页
Javascript
Html
Css
Node.js
Electron
移动开发
工具类
服务端
浏览器相关
前端收藏
其他

获取当前时间、日期、星期(兼容IE,Firefox,Chrome,Opera,Safari...)

2012年10月22日 发布 阅读(394) 作者:懒炖

获取当前时间代码,用原生JS所写,兼容种浏览器,代码如下:

  1. function Year_Month() {
  2. var now = new Date();
  3. var yy = now.getYear();
  4. var mm = now.getMonth();
  5. var mmm = new Array();
  6. mmm[0] = "1";
  7. mmm[1] = "2";
  8. mmm[2] = "3";
  9. mmm[3] = "4";
  10. mmm[4] = "5";
  11. mmm[5] = "6";
  12. mmm[6] = "7";
  13. mmm[7] = "8";
  14. mmm[8] = "9";
  15. mmm[9] = "10";
  16. mmm[10] = "11";
  17. mmm[11] = "12";
  18. mm = mmm[mm];
  19. return (mm);
  20. }
  21. function thisYear() {
  22. var now = new Date();
  23. var yy = now.getFullYear();
  24. return (yy);
  25. }
  26. function Date_of_Today() {
  27. var now = new Date();
  28. return (now.getDate());
  29. }
  30. function Date_of_Week() {
  31. var now = new Date();
  32. var week = now.getDay();
  33. var week_day
  34. if (week == 1) {
  35. week_day = "星期一"
  36. };
  37. if (week == 2) {
  38. week_day = "星期二"
  39. };
  40. if (week == 3) {
  41. week_day = "星期三"
  42. };
  43. if (week == 4) {
  44. week_day = "星期四"
  45. };
  46. if (week == 5) {
  47. week_day = "星期五"
  48. };
  49. if (week == 6) {
  50. week_day = "星期六"
  51. };
  52. if (week == 0) {
  53. week_day = "星期日"
  54. };
  55. return (week_day);
  56. }
  57. function CurentTime() {
  58. var now = new Date();
  59. var hh = now.getHours();
  60. var mm = now.getMinutes();
  61. var ss = now.getTime() % 60000;
  62. ss = (ss - (ss % 1000)) / 1000;
  63. var clock = hh + ':';
  64. if (mm < 10) clock += '0';
  65. clock += mm + ':';
  66. if (ss < 10) clock += '0';
  67. clock += ss;
  68. return (clock);
  69. }
  70. document.write('今天是<span id="clock" ></span>');
  71. var clock = document.getElementById("clock");
  72. function refreshCalendarClock() {
  73. var clockHTML = "";
  74. clockHTML += thisYear() + "年";
  75. clockHTML += Year_Month() + "月";
  76. clockHTML += Date_of_Today() + "日";
  77. clockHTML += CurentTime();
  78. clockHTML += Date_of_Week();
  79. clock.innerHTML = clockHTML;
  80. }
  81. setInterval('refreshCalendarClock()', 1000);
版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处

评论

  •