vue获取当前年月日时分秒

// 获取当前时间
getNowTime(isAll){
    let now = new Date();
    let year = now.getFullYear(); //获取完整的年份(4位,1970-????)
    let month = now.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    let today = now.getDate(); //获取当前日(1-31)
    let hour = now.getHours(); //获取当前小时数(0-23)
    let minute = now.getMinutes(); //获取当前分钟数(0-59)
    let second = now.getSeconds(); //获取当前秒数(0-59)
    let nowTime = ''
    //返回年月日时分秒
    if(isAll){
    nowTime = year + '-' + this.fillZero(month) + '-' + this.fillZero(today) + ' ' + this.fillZero(hour) + ':' + this.fillZero(minute) + ':' + this.fillZero(second)
    }else{//返回年月日
      nowTime = year + '-' + this.fillZero(month) + '-' + this.fillZero(today)
    }
    return nowTime
},
          // 给时间补零
  fillZero(str){
    var realNum;
    if (str < 10) {
        realNum = '0' + str;
    } else {
        realNum = str;
    }
    return realNum;
},


console.log(this.getNowTime(true));//控制台打印:2024-01-02 08:16:22
console.log(this.getNowTime(false));//控制台打印:2024-01-02

你可能感兴趣的:(vue.js,前端,javascript)