vue3获取当前日期、时间和星期(格式:年月日 时分秒 星期)

您所在的位置:网站首页 js实时显示当前时间 vue3获取当前日期、时间和星期(格式:年月日 时分秒 星期)

vue3获取当前日期、时间和星期(格式:年月日 时分秒 星期)

2023-06-18 22:43| 来源: 网络整理| 查看: 265

           

创建时间:{{ date }}

        import { ref, onMounted } from 'vue'

const date = ref('')   //格式化时间的函数  。它接受一个数字类型的参数 time,表示需要格式化的时间。   //如果 time 小于10,则返回一个带有前导零的字符串表示;   //否则,将 time 转换为字符串后返回。 function formatTime(time: number) {     return time < 10 ? `0${time}` : time } function updateTime() {     const now = new Date()     const year = now.getFullYear()  //年     const month = now.getMonth() + 1 //月     const day = now.getDate()    //日     const hours = now.getHours()  //小时数     const minutes = now.getMinutes()  //分钟数     const seconds = now.getSeconds()  //秒数     const week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"][now.getDay()]  //星期   //想展示什么  对应的展示即可    date.value = `${year}-${formatTime(month)}-${formatTime(day)} ${formatTime(hours)}:${formatTime(minutes)}:${formatTime(seconds)} ${week}` }   onMounted(() => {     updateTime() })



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3