1. <script>
    2. //作用求两个日期时间的时间间隔
    3. function getSpaceBetweenDate(date1,date2) {
    4. //1.得到两个日期之间的秒数
    5. var between = Math.round(Math.abs(date1 - date2)/1000);
    6. //2.把秒数转换成相差多少天 多少小时 多少分钟 多少秒
    7. var day = Math.floor(between/(24*60*60));
    8. var hour = Math.floor(between/60/60) % 24;
    9. var minute = Math.floor(between/60)%60;
    10. var second = between%60;
    11. //3.包装一个对象返回
    12. var obj = {
    13. day:day,
    14. hour:hour,
    15. minute:minute,
    16. second:second
    17. }
    18. return obj;
    19. }
    20. var date1 = new Date("2017-8-8 22:23:23");
    21. var date2 = new Date("2017-10-8 23:30:30");
    22. //使用自己写的方法求两个时间的差
    23. var o = getSpaceBetweenDate(date1,date2);
    24. console.log(o);
    25. </script>

    image.png