js gets the timestamp from 0:00 to 24:00

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>js获取当天时间0点到24点的时间戳</title>
  </head>
  <body>
    <script>
      //获取格林时间
      var date1 = new Date(new Date(new Date().toLocaleDateString()).getTime());
      var date2 = new Date(
        new Date(new Date().toLocaleDateString()).getTime() +
          24 * 60 * 60 * 1000 -
          1
      );
      console.log(date1, date2);

      //格式化时间  2020-10-10 00:00:00
      var startTime =
        date1.getFullYear() +
        "-" +
        (date1.getMonth() + 1 < 10
          ? "0" + (date1.getMonth() + 1)
          : date1.getMonth() + 1) +
        "-" +
        (date1.getDate() < 10 ? "0" + date1.getDate() : date1.getDate()) +
        " " +
        (date1.getHours() < 10 ? "0" + date1.getHours() : date1.getHours()) +
        ":" +
        (date1.getMinutes() < 10
          ? "0" + date1.getMinutes()
          : date1.getMinutes()) +
        ":" +
        (date1.getSeconds() < 10
          ? "0" + date1.getSeconds()
          : date1.getSeconds());
      console.log(startTime);

      //格式化时间  2020-10-10 23:59:59
      var endTime =
        date2.getFullYear() +
        "-" +
        (date2.getMonth() + 1) +
        "-" +
        date2.getDate() +
        " " +
        date2.getHours() +
        ":" +
        date2.getMinutes() +
        ":" +
        date2.getSeconds();
      console.log(endTime);
    </script>
  </body>
</html>

Print result
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43248623/article/details/109000008