首先,确定想要的时间格式,然后进行处理(以下的格式是yy-mm-dd hh-mm-ss,如:2021-10-09 08:58:30)
Vue.prototype.CustomMethods.getCurrentTime = function (d) {
let datetime = new Date()
if (d){
datetime = new Date(d)
}
let year = datetime.getFullYear()
let month = datetime.getMonth() + 1
let date = datetime.getDate()
let hour = datetime.getHours()
let minutes = datetime.getMinutes()
let second = datetime.getSeconds()
//月,日,时,分,秒 小于10时,补0
if (month < 10) {
month = "0" + month
}
if (date < 10) {
date = "0" + date
}
if (hour < 10) {
hour = "0" + hour
}
if (minutes < 10) {
minutes = "0" + minutes
}
if (second < 10) {
second = "0" + second
}
let time = `${year}-${month}-${date} ${hour}:${minutes}:${second}`
return time
}
然后,在需要获取当前时间的地方运用
最后,把currentTime在页面上显示