JS 获取当前时间 当前日期

81 阅读1分钟
 getTime () {
      const date = new Date();
      // 时、分、秒
      let hour = date.getHours();
      let minute = date.getMinutes();
      let second = date.getSeconds();
      hour = hour > 9 ? hour : "0" + hour
      minute = minute > 9 ? minute : "0" + minute
      second = second > 9 ? second : "0" + second
      const str = hour + ":" + minute + ":" + second
      return str
    },
    
  getDate () {
      const date = new Date()
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDay()
      const _date = date.getDate()
      const chinese = ['日', '一', '二', '三', '四', '五', '六']
      const str = year + "年" + month + "月" + _date + "日" + "  星期" + chinese[day]
      return str
    }

在pc端直接使用 computed 计算属性就行,在uniapp中需要使用定时器循环调用