// 显示i当前时间
showTime() {
// myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
// myDate.getMilliseconds(); //获取当前毫秒数(0-999)
// myDate.toLocaleDateString(); //获取当前日期
// myDate.toLocaleTimeString(); //获取当前时间
// myDate.toLocaleString( ); //获取日期与时间
let _this = this
let myDate = new Date();
let year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
let month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
let day = myDate.getDate(); //获取当前日(1-31)
let week = myDate.getDay();//获取当前星期X(0-6,0代表星期天)
let YMD = year + '年' + this.addNum(month) + '月' + this.addNum(day) + '日 '
let transWeek = this.getWeek(week)
//函数里边只写时,分,秒,每秒都请求一次年、月、日、星期是不必要的。
setInterval(function () {
let myDate = new Date();
let hours = myDate.getHours(); //获取当前小时数(0-23)
let minutes = myDate.getMinutes(); //获取当前分钟数(0-59)
let seconds = myDate.getSeconds(); //获取当前秒数(0-59)
_this.nowTime = YMD + _this.addNum(hours) + ':' + _this.addNum(minutes) + ':' + _this.addNum(seconds) +' '+ transWeek
}, 1000)
},
// 补0函数
addNum(num) {
if (num.toString().length < 2) {
num = '0' + num;
}
return num
},
// 获取星期几
getWeek(num){
switch (num) {
case 0 :{
return '星期天'
}
case 1:{
return '星期一'
}
case 2:{
return '星期二'
}
case 3:{
return '星期三'
}
case 4:{
return '星期四'
}
case 5:{
return '星期五'
}
case 6:{
return '星期六'
}
}
}
原生js实时显示当前年月日时分秒星期
最新推荐文章于 2024-06-20 14:17:06 发布