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

    返回当前时间:

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

    输出实时时间(秒可以变化):

    1. <body>
    2. <div id="app"></div>
    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. }
    15. function handleNum(num) {
    16. if (num < 10) {
    17. num = "0" + num;
    18. }
    19. return num;
    20. }
    21. </script>
    22. </body>

    距离活动开始时间:小时分钟秒

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

    把给定的time时间(毫秒)转换为几小时几分钟几秒:

    1. <script>
    2. var time = 360000;
    3. /*
    4. 66s
    5. */
    6. /*
    7. 一秒
    8. 60s 一分钟
    9. 60*60 一小时
    10. time/1000 秒
    11. */
    12. var seconds = Math.floor(time/1000);
    13. console.log(seconds)
    14. var hours = Math.floor(seconds/3600);
    15. console.log(hours)
    16. // seconds = seconds- hours*3600;
    17. seconds = seconds%3600;
    18. console.log(seconds)
    19. var minutes = Math.floor(seconds/60);
    20. console.log(minutes)
    21. seconds = seconds- minutes*60;
    22. console.log(seconds)
    23. </script>
    1. <script>
    2. var time = 306000;
    3. /* 3000
    4. 1m = 60s
    5. */
    6. var seconds = time/1000;
    7. console.log(seconds+"秒")
    8. var minutes = Math.floor(seconds/60);
    9. var seconds = seconds - minutes*60;
    10. console.log(minutes+"分")
    11. console.log(seconds+"余下的秒")
    12. /* 3s */
    13. </script>
    1. <script>
    2. var time = 306150000;
    3. /* 3000
    4. 1m = 60s
    5. 1h = 60*60s
    6. */
    7. var seconds = time/1000;
    8. console.log(seconds+"秒")
    9. var hours = Math.floor(seconds/3600);
    10. console.log(hours)
    11. var seconds = seconds%3600;
    12. console.log(seconds)
    13. var minutes= Math.floor(seconds/60);
    14. console.log(minutes)
    15. seconds = seconds%60;
    16. console.log(seconds)
    17. /* 3s */
    18. </script>

    现在距离10.19还有几天+几小时+几分钟:

    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>