美文网首页
web-jianshu Date日期格式转换

web-jianshu Date日期格式转换

作者: web_jianshu | 来源:发表于2020-05-13 09:07 被阅读0次

    <!DOCTYPE html>

    <html lang="en">

      <head>

        <meta charset="UTF-8" />

        <title>Title</title>

      </head>

      <body>

        <script>

          // 第一种 获取当前时间

          var date1 = new Date();

          console.log("第一种", date1);

          // => 第一种 Tue May 12 2020 11:27:25 GMT+0800 (中国标准时间)

          // 第二种 指定时间

          var date2 = new Date("2018/06/13 00:00:00");

          console.log("第二种", date2);

          // => 第二种 Wed Jun 13 2018 00:00:00 GMT+0800 (中国标准时间)

          //后两种不推荐使用 兼容性不好

          // 第三种 获取当前时间

          var date3 = new Date("Wed Jun 13 2018 00:00:00 GMT+0800 (中国标准时间)");

          console.log("第三种", date3);

          // => 第三种 Wed Jun 13 2018 00:00:00 GMT+0800 (中国标准时间)

          // 第四种

          var date4 = new Date(2018, 6, 12);

          console.log("第四种", date4);

          // => 第四种 Thu Jul 12 2018 00:00:00 GMT+0800 (中国标准时间)

          // 第五种

          var date5 = new Date("2020-05-12 11:12:52");

          console.log("第五种", date5);

          // => 第五种 Tue May 12 2020 11:12:52 GMT+0800 (中国标准时间)

          console.log(date1.getDate()); //获取日1-31

          console.log(date1.getDay()); //获取星期0-6(0代表周日)

          console.log(date1.getMonth()); //获取月0-11(1月从0开始)

          console.log(date1.getFullYear()); //获取完整年份(浏览器都支持)

          console.log(date1.getHours()); //获取小时0-23

          console.log(date1.getMinutes()); //获取分钟0-59

          console.log(date1.getSeconds()); //获取秒0-59

          console.log(date1.getMilliseconds()); //获取毫秒(1s=1000ms)

          console.log(date1.getTime()); //返回累计毫秒数(从1970/1/1午夜)

          //获取当前时间距离1970/1/1毫秒数

          var date11 = Date.now();

          var date22 = +new Date();

          var date33 = new Date().getTime();

          var date44 = new Date().valueOf();

          // 返回1970年1月1日午夜与当前日期和时间之间的毫秒数

          console.log(date11);

          console.log(date22);

          console.log(date33);

          console.log(date44);

        </script>

      </body>

    </html>

    相关文章

      网友评论

          本文标题:web-jianshu Date日期格式转换

          本文链接:https://www.haomeiwen.com/subject/slxbnhtx.html