js方法获取当前时间
getNowFormatDate() {
let date = new Date(),
seperator1 = '-', //格式分隔符
year = date.getFullYear(), //获取完整的年份(4位)
month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
strDate = date.getDate(), // 获取当前日(1-31)
hour = date.getHours(), // 时
minutes = date.getMinutes(), //分
seconds = date.getSeconds(); //秒
if (month >= 1 && month <= 9) month = '0' + month // 如果月份是个位数,在前面补0
if (strDate >= 0 && strDate <= 9) strDate = '0' + strDate // 如果日是个位数,在前面补0
let currentdate = year + seperator1 + month + seperator1 + strDate+ " " + hour + ":" + minutes + ":" + seconds;
return currentdate;
},
上面方法获取到的时间格式是 yyyy-MM-dd HH:mm:ss;需要其他格式时,对应的修改currentdate所拼接的值就好
使用:
getTime() {
//获取当前时间 yyyy-MM-dd HH:mm:ss
let time = this.getNowFormatDate();
}