1. <script>
  2. var date = new Date();
  3. //年月日
  4. console.log(date)
  5. var year = date.getFullYear();
  6. console.log(year)
  7. //月份从0开始
  8. var month = date.getMonth()+1;
  9. console.log(month)
  10. var data = date.getDate();
  11. console.log(data)

时间表

  1. p>14:14:10</p>
  2. <script>
  3. var d= new Date();
  4. var hours = d.getHours();
  5. var minutes = d.getMinutes();
  6. var seconds = d.getSeconds();
  7. console.log(hours)
  8. console.log(minutes)
  9. console.log(seconds)
  10. </script>

当前时间在html页面上

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

算时间

  1. <script>
  2. var time = "2021-10-13 17:10:10"
  3. /*
  4. 多少个小时,多少分钟,多少秒
  5. 1获取当前时间的毫秒数
  6. */
  7. var d = new Date();
  8. //console.log(d.getTime())
  9. //2获取活动开始时间的毫秒数
  10. var startTime = d.getTime
  11. var endTime = new Date(time).getTime();
  12. //console.log(endTime)
  13. //3得到中间间隔的毫秒数
  14. var gapTime = endTime-startTime;
  15. //console.log(gapTime)
  16. var seconds = Math.floor(gapTime/1000);
  17. console.log(seconds)
  18. var hours = Math.floor(seconds/3600);
  19. console.log(hours)
  20. seconds = seconds -3600*hours;
  21. var minutes = Math.floor(seconds/60);
  22. console.log(minutes)
  23. seconds = seconds-minutes*60;
  24. console.log(seconds)
  25. </script>
  1. <script>
  2. var time = "2021-10-13 17:10:10"
  3. /*
  4. 多少个小时,多少分钟,多少秒
  5. 1获取当前时间的毫秒数
  6. */
  7. var d = new Date();
  8. //console.log(d.getTime())
  9. //2获取活动开始时间的毫秒数
  10. var startTime = d.getTime
  11. var endTime = new Date(time).getTime();
  12. //console.log(endTime)
  13. //3得到中间间隔的毫秒数
  14. var gapTime = endTime-startTime;
  15. //console.log(gapTime)
  16. var seconds = Math.floor(gapTime/1000);
  17. console.log(seconds)
  18. var hours = Math.floor(seconds/3600);
  19. console.log(hours)
  20. seconds = seconds -3600*hours;
  21. var minutes = Math.floor(seconds/60);
  22. console.log(minutes)
  23. seconds = seconds-minutes*60;
  24. console.log(seconds)
  25. </script>
  1. <script>
  2. var time = 306000;
  3. //3000毫秒
  4. var seconds = time/1000;
  5. console.log(seconds+"秒")
  6. var seconds = seconds-minutes*60
  7. //几分钟
  8. var minutes =Math.floor(seconds/60);
  9. console.log(minutes+"分")
  10. //3s
  11. console.log(seconds+"余下的秒")
  1. <script>
  2. var time = 306050000;
  3. /*
  4. 3000
  5. 1m =60s
  6. 1h = 60*60s
  7. */
  8. var seconds = time/1000;
  9. console.log(seconds+"秒")
  10. var hours =Math.floor(seconds/3600);
  11. console.log(hours+"小时")
  12. var seconds = seconds%3600;
  13. console.log(seconds)
  14. var minutes =Math.floor(seconds/60);
  15. console.log(minutes)
  16. seconds =seconds%60;
  17. console.log(seconds)
  18. </script>
  1. <script>
  2. var time = "2021-10-13 21:30:00"
  3. var d = new Date();
  4. //console.log(d.getTime())
  5. var startTime = d.getTime();
  6. //获取活动开始时间的毫秒数
  7. var endTime = new Date(time).getTime();
  8. //console.log(endTime)
  9. //3 得到中间间隔的毫秒数
  10. var gapTime = endTime -startTime;
  11. var seconds = Math.floor(gapTime/1000)
  12. console.log(seconds+"秒")
  13. var hours = Math.floor(seconds/3600);
  14. console.log(hours)
  15. seconds = seconds - 3600*hours;
  16. var minutes = Math.floor(seconds/60);
  17. console.log(minutes)
  18. seconds = seconds -minutes*60;
  19. console.log(seconds)
  20. </script>
  1. <script>
  2. var time = "2021-10-19 08:30:00"
  3. var startTime = (new Date()).getTime();
  4. //console.log(startTime)
  5. var endTime = (new Date(time)).getTime();
  6. var gapTime = endTime-startTime;
  7. //console.log(gapTime/1000/60)
  8. var minutes =Math.floor(gapTime/(1000*60));
  9. //console.log(minutes)
  10. /*
  11. 8201分
  12. 1h =60m
  13. 1t = 60*24
  14. */
  15. var days =Math.floor(minutes/(60*24));
  16. minutes = minutes -days*(60*24)
  17. console.log(days)
  18. var hours = Math.floor(minutes/60)
  19. console.log(hours)
  20. minutes = minutes-hours*60;
  21. console.log(minutes)
  22. </script>