1.获取年月日时分秒
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let strDate = date.getDate();
let hours = date.getHours()
let minutes = date.getMinutes()
let seconds = date.getSeconds()
if (month >= 1 && month <= 9) { month = "0" + month; }
if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; }
console.log(year + '年' + month + '月' + strDate+'日'+hours+'时'+minutes+'分'+seconds+'秒')
效果
2.获取当前时间前后30分钟
created() {
let date = new Date();//23.59
console.log(date)
let month = date.getMonth() + 1;//月份
let strDate = date.getDate();//天
let hoursStart = date.getHours();//小时
let hoursEnd = date.getHours()
let minutesStart = date.getMinutes() - 30;
let minutesEnd = date.getMinutes() + 30;
if (month >= 1 && month <= 9) { month = "0" + month; }
if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; }
// 00 : 00 -1 23:10
if (minutesStart < 0) {
hoursStart = hoursStart - 1;
minutesStart = minutesStart + 60;
if (hoursStart < 0) {
this.rangeStart.selectableRange = (`${hoursEnd}:00:00 - ${hoursEnd}:${minutesEnd}:00`);
}
this.rangeStart.selectableRange = (`${hoursStart}:${minutesStart}:00 - ${hoursEnd}:${minutesEnd}:00`)
} else if (minutesEnd > 60) {
// 23 : 50 24 : 80
let hoursStart = 23;
hoursStart = hoursStart + 1;
minutesStart = Number(minutesEnd) - 60;
if (hoursStart > 23) {
this.rangeStart.selectableRange = (`${hoursEnd}:${minutesStart}:00 -23:59:59`);
}
this.rangeStart.selectableRange = (`${hoursEnd}:${minutesStart}:00 - ${hoursStart}:${minutesStart}:00`)
console.log(this.rangeStart.selectableRange)
}
},