// 存储实时时间
let time = ref<any>()
// 时间处理
let nowDate = (now:any)=>{
let year = String(now.getFullYear()); //年份
let month = String(now.getMonth() + 1); //月份
let date = String(now.getDate()); //天数
let hour = String(now.getHours()<10?'0'+now.getHours():now.getHours()); //小时
let minute = String(now.getMinutes()<10?'0'+now.getMinutes():now.getMinutes()); //分钟数
let second = String(now.getSeconds()<10?'0'+now.getSeconds():now.getSeconds()); //秒数
let wk = now.getDay() //星期
let weeks = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']
let week = weeks[wk]
return {
nowDate:`${year}-${month}-${date}`,
nowTime:`${hour}:${minute}:${second}`,
nowWeek:week
}
}
//获取现在的时间回调
let getDate = () => {
const a = new Date().getTime() //获取当前时间戳
const b = new Date(a) //创建时间对象
time.value = nowDate(b) //处理时间格式函数
// console.log('time.value', time.value)
// time.value = moment().format('YYYY-MM-DD HH:mm:ss'); 插件测试也会停顿 暂时注释保留
}
// 定时器
const timer = ref()
onMounted(() => {
getDate()
// 时间定时器,每秒钟请求一次
timer.value = setInterval(() => {
getDate()
}, 1000)
})
onUnmounted(() => {
// 清除定时器
clearInterval(timer.value)
})
日期时间格式化处理
于 2023-08-30 15:05:17 首次发布