js获取当前时间、年月日、星期几

      let date = new Date()

      // 获取时间:时分秒
      const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
      const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
      const secound = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
      this.datetime = hour + ':' + minute + ':' + secound

      // 获取日期:年月日
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      this.nowDate = month + "月" + day + "日"
      this.nowYear = year + "年"

      // 获取星期几
      const weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
      this.nowWeek = weeks[new Date().getDay()];

效果如下:

在这里插入图片描述
完整代码

原文地址


版权声明:本文为qq_14994863原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。