显示时间到页面上

  1. <div id="app"></div>
  2. <script>
  3. setInterval(show,1000)
  4. function show() {
  5. var app = document.getElementById("app")
  6. var d = new Date();
  7. var hours = d.getHours();
  8. var minutes = d.getMinutes();
  9. var seconds = d.getSeconds();
  10. var sum = handleNum(hours) + ":" + handleNum(minutes) + ":" + handleNum(seconds);
  11. console.log(sum)
  12. app.innerHTML = sum;
  13. }
  14. function handleNum(num) {
  15. if (num < 10) {
  16. num = "0" + num;
  17. }
  18. return num;
  19. }
  20. </script>

距离下个时间 多少小时 多少分钟 多少秒

  1. var time = "2021-10-13 17:10:10"
  2. /* 多个小时,多分钟,多少秒 */;
  3. /* 1、获取当前时间的毫秒数 */
  4. var d = new Date();
  5. // console.log(d.getTime())
  6. var startTime = d.getTime();
  7. /* 2、获取活动开始时间的毫秒 */
  8. var endTime = new Date(time).getTime();
  9. // console.log(endTime)
  10. /* 3、得到中间间隔的毫秒数 */
  11. var gapTime = endTime -startTime;
  12. var seconds = Math.floor(gapTime/1000)
  13. console.log(seconds)
  14. var hours = Math.floor(seconds/3600);
  15. console.log(hours)
  16. seconds = seconds - 3600*hours;
  17. var minutes = Math.floor(seconds/60);
  18. console.log(minutes)
  19. seconds = seconds - minutes*60;
  20. console.log(seconds)

距离某天还有几天几小时几分

  1. <script>
  2. /* var time ="2021-10-19 08:30:00" 还有几天几小时几分 */
  3. var time = "2021-10-19 08:30:00"
  4. var startTime = (new Date()).getTime();
  5. var endTime = (new Date(time)).getTime();
  6. var gapTime = endTime-startTime;
  7. var minutes = Math.floor(gapTime/(1000*60));
  8. /*
  9. 8201分
  10. 1h = 60m
  11. 1t = 60*24
  12. */
  13. // console.log(minutes)
  14. var days = Math.floor(minutes/(60*24));
  15. minutes = minutes -days*(60*24)
  16. console.log(days)
  17. var hours = Math.floor(minutes/60);
  18. console.log(hours)
  19. minutes = minutes - hours*60;
  20. console.log(minutes)
  21. </script>