1. <script>
    2. //获取当前时间
    3. var date = new Date();
    4. //格式化日期
    5. function formatDate(date) {
    6. var seconds = date.getSeconds();
    7. var minute = date.getMinutes();
    8. var hour = date.getHours();
    9. var day = date.getDate();
    10. var month = date.getMonth()+1;
    11. var year = date.getFullYear();
    12. seconds = seconds<10?"0"+seconds:seconds;
    13. minute = minute<10?"0"+minute:minute;
    14. hour = hour<10?"0"+hour:hour;
    15. day = day<10?"0"+day:day;
    16. month = month<10?"0"+month:month;
    17. return str = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+seconds;
    18. }
    19. var dates = formatDate(date);
    20. console.log(dates);
    21. </script>

    image.png