// 时间处理函数functiongetTime(){var nowTime =newDate()// 获取年月日const y = nowTime.getFullYear()const m =zero(nowTime.getMonth()+1)const d =zero(nowTime.getDate())// 获取时分秒const hh =zero(nowTime.getHours())const mm =zero(nowTime.getMinutes())const ss =zero(nowTime.getSeconds())// 获取星期const week =we(nowTime.getDay())// 时间补零函数functionzero(time){if(time <10){return'0'+ time
}return time
}// 星期处理函数functionwe(week){switch(week){case0:return'星期日'case1:return'星期一'case2:return'星期二'case3:return'星期三'case4:return'星期四'case5:return'星期五'case6:return'星期六'default:// 可选return'错误'// 语句}}return(
y +'/'+ m +'/'+ d +' '+ hh +':'+ mm +':'+ ss +' '+ week
)}// 想拿到的话直接这样const time =getTime()// 就行了
vue中用法
exportdefault{// 组件名称
name:'',
props:{// 组件参数 接收来自父组件的数据},
components:{// 注册组件},data(){return{
time:null}},
methods:{// 方法handler(){setInterval(()=>{this.time =this.getTime()},1000)},// 时间处理函数getTime(){var nowTime =newDate()// 获取年月日const y = nowTime.getFullYear()const m =zero(nowTime.getMonth()+1)const d =zero(nowTime.getDate())// 获取时分秒const hh =zero(nowTime.getHours())const mm =zero(nowTime.getMinutes())const ss =zero(nowTime.getSeconds())// 获取星期const week =we(nowTime.getDay())// 时间补零函数functionzero(time){if(time <10){return'0'+ time
}return time
}// 星期处理函数functionwe(week){switch(week){case0:return'星期日'case1:return'星期一'case2:return'星期二'case3:return'星期三'case4:return'星期四'case5:return'星期五'case6:return'星期六'default:// 可选return'错误'// 语句}}return(
y +'/'+ m +'/'+ d +' '+ hh +':'+ mm +':'+ ss +' '+ week
)}},
computed:{// 计算属性},
filters:{// 过滤器},created(){// 创建后},mounted(){// 挂载后this.handler()}}