JavaScript:时间对象,实例演示右下角广告图

一.时间对象实例

方法含义
new Date()获取时间
getDate()获取日数
getMonth()获取月份
getFullYear()获取年份
getHours()获取小时
getMinutes()获取分钟数
getSeconds()获取秒数
getDay()获取星期几

代码块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>获取年月日</title>
  8. <style>
  9. :root {
  10. height: 2000px;
  11. }
  12. .box {
  13. background-color: lightblue;
  14. height: 40px;
  15. width: 250px;
  16. display: flex;
  17. justify-content: center;
  18. align-items: center;
  19. color: #666;
  20. position: fixed;
  21. right: 0;
  22. bottom: 80px;
  23. }
  24. .box > nav {
  25. margin-right: 1em;
  26. }
  27. .box > button {
  28. position: absolute;
  29. right: 0;
  30. top: 0;
  31. }
  32. .box > div {
  33. position: fixed;
  34. top: auto;
  35. bottom: 0;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="box">
  41. <nav></nav>
  42. <span></span>
  43. <button>x</button>
  44. <div>
  45. <a href=""
  46. ><img
  47. src="http://imga3.5054399.com/upload_pic/2021/4/21/4399_09142332383.jpg"
  48. alt=""
  49. /></a>
  50. </div>
  51. </div>
  52. <script>
  53. //get 是获取 set 是设置
  54. let d = new Date();
  55. // getDate 获取日数
  56. let myDay = d.getDate();
  57. //getMonth 获取月份
  58. let myMonth = d.getMonth() + 1;
  59. //getFullYear 获取年份
  60. let myYear = d.getFullYear();
  61. //getHours 获取小时
  62. let myHours = d.getHours();
  63. //getMinutes 获取分钟数
  64. let myMinutes = d.getMinutes();
  65. //getSeconds 获取秒数
  66. let mySeconds = d.getSeconds();
  67. //getDay 获取星期几
  68. let myGetday = d.getDay();
  69. let week = [
  70. "星期一",
  71. "星期二",
  72. "星期三",
  73. "星期四",
  74. "星期五",
  75. "星期六",
  76. "星期日",
  77. ];
  78. // 获取.box 内的nav 元素
  79. document
  80. .querySelector(".box > nav")
  81. .append(myYear + "年" + myMonth + "月" + myDay + "日");
  82. // 获取.box 内的span 元素
  83. document.querySelector(".box > span").append(week[myGetday]);
  84. //按钮添加功能 remove 移除掉 box盒子 等于关闭掉这个提示
  85. let box = document.querySelector(".box");
  86. let btn = document.querySelector(".box > button");
  87. btn.addEventListener("click", (ev) => {
  88. ev.target = box.remove();
  89. });
  90. //判断当前时间点,输出 早上好 晚上好 下午好
  91. if (myHours < 12) {
  92. document.body.append("早上好");
  93. } else if (myHours >= 12 && myHours < 18) {
  94. document.body.append("下午好");
  95. } else {
  96. document.body.append("晚上好");
  97. }
  98. </script>
  99. </body>
  100. </html>

更多相关文章

  1. Spring 获取 request 的几种方法及其线程安全性分析
  2. echarts 超过预警值后节点变红预警
  3. Map集合封装获取参数
  4. 快递物流接口API如何获取第三方电商平台账号授权案例
  5. List集合封装获取参数
  6. Cocos Creator之摇杆的实现
  7. java中Locks的使用
  8. 初学Redis最清晰完整的教程
  9. Servlet系列教材 (二)- 基础 - 如何获取浏览器提交的参数

随机推荐

  1. 开发中常用到的几处代码设置
  2. [AndroidTips]Keep Screen On in Android
  3. android案例
  4. asdsadsa
  5. android 去掉listview之间的黑线
  6. android各大分享平台地址
  7. Android自定义九宫格图案解锁
  8. android输入法全屏问题
  9. Android中自定义DatePicker
  10. Anfdroid网络编程方法