var date = new Date()
var year = date.getFullYear()//年
var month = date.getMonth() + 1//月
var strDate = padDate(date.getDate())//日
var Hours = padDate(date.getHours())//时
var Minutes = padDate(date.getMinutes())//分
if (month >= 1 && month <= 9) {
month = ‘0’ + month
}
// if (strDate >= 0 && strDate <= 9) {
// strDate = “0” + strDate;
// }
Minutes = (Minutes <= 9) ? ‘0’ + Minutes : Minutes
var currentdate = year + ‘年’ + month + ‘月’ + strDate + ‘日’ + ’ ’ + Hours + ‘:’ + Minutes
this.period = this.getPeriod(Hours)//传时进来进行判断当前时间是属于上午还是下午
this.todayData = currentdate//给拼接好的日期赋值
},
getPeriod(val) {//传时进来进行判断当前时间是属于上午还是下午
if (val >= 3 && val < 6) {//凌晨:3:00–6:00
return ‘凌晨’
} else if (val >= 6 && val < 8) {//早晨:6:00—8:00
return ‘早晨’
} else if (val >= 8 && val < 11) {//上午:8:00–11:00
return ‘上午’
} else if (val >= 11 && val < 13) {//中午:11:00–13:00
return ‘中午’
} else if (val >= 13 && val < 17) {//下午:13:00–17:00
return ‘下午’
} else if (val >= 17 && val < 19) {//傍晚:17:00–19:00
return ‘傍晚’
} else if (val >= 19 && val < 23) {//晚上:19:00–23:00
return ‘晚上’
} else {//深夜:23:00–3:00
return ‘深夜’
}
},
在return里保存的值 根据时间段来判断处于哪个阶段(早晨,中午,傍晚。。。)
获得当前年月日时分秒并能判断是早晨 中午 下午 深夜
最新推荐文章于 2023-12-25 10:29:18 发布