时间选择器:
需求:设置课程时段,时刻A与时刻B会存在前后关系,选择时刻B时,可选的最小时段为时刻A+1分钟。选择分钟00时,小时显示为06。时刻A:startTime 时段B:endTime
startTime(time) {
let starthour = time.slice(0, 2);
let startMinute = time.slice(3, 5);
let endhour = this.endTime.slice(0, 2);
let endMinute = this.endTime.slice(3, 5);
if (!starthour && !startMinute) {
return (this.startTime = "");
}
if (!starthour || starthour == "00") {
return (this.startTime = "06:00");
}
if (this.endTime && endhour == starthour) {
if (startMinute >= endMinute) {
endMinute = Number(startMinute) + 1;
time = `${starthour}:${endMinute}`;
this.endTime = time;
}
}
},
endTime(time) {
let endhour = time.slice(0, 2);
let endMinute = time.slice(3, 5);
let starthour = this.formItems.startTime.slice(0, 2);
let startMinute = this.formItems.startTime.slice(3, 5);
if (!endhour && !endMinute) {
return (this.formItems.endTime = "");
}
if (!endhour || endhour == "00") {
return (this.formItems.endTime = "06:00");
}
if (this.formItems.startTime && endhour == starthour) {
if (startMinute >= endMinute) {
endMinute = Number(startMinute) + 1;
time = `${endhour}:${endMinute}`;
this.formItems.endTime = time;
}
}
}
时间格式转化:
convertUTCTimeToLocalTime(UTCDateString) {
if (!UTCDateString) {
return "-";
}
function formatFunc(str) {
//格式化显示
return str > 9 ? str : "0" + str;
}
let date2 = new Date(UTCDateString);
let year = date2.getFullYear();
let mon = formatFunc(date2.getMonth() + 1);
let day = formatFunc(date2.getDate());
let dateStr = year + "-" + mon + "-" + day;
return dateStr;
},
时间戳的转换:(将日期与时段拼接起来转化为时间戳)
const date = this.convertUTCTimeToLocalTime(this.minScheduleTime);
const beginSchedule = +new Date(`${date} ${this.startTime}`);
const endSchedule = +new Date(`${date} ${this.endTime}`);
小于10月的月份前加“0”:
padZero(num) {
return num < 10 ? `0${num}` : num;
},
日期(例如:2020-09-20)------ 获取增加n天后的日期:
dateStr:当前时间,dayCount:固定天数
dateAddDays(dateStr, dayCount, index) {
if (index == "0") {
return dateStr;
} else {
const tempDate = new Date(dateStr.replace(/-/g, "/")); //把日期字符串转换成日期格式
const resultDate = new Date((tempDate / 1000 + 86400 * dayCount) * 1000); //增加n天后的日期
const year = resultDate.getFullYear();
const mounth = resultDate.getMonth() + 1;
const day = resultDate.getDate();
const resultDateStr = `${year}-${this.padZero(mounth)}-${this.padZero(day)}`;
return resultDateStr;
}
},