2025-07-15 16:18:34 +08:00
|
|
|
"use strict";
|
|
|
|
|
const common_vendor = require("../vendor.js");
|
|
|
|
|
common_vendor.dayjs.locale("zh-cn");
|
|
|
|
|
common_vendor.dayjs.extend(common_vendor.relativeTime);
|
2025-12-15 18:19:04 +08:00
|
|
|
function showDay(time) {
|
|
|
|
|
return common_vendor.dayjs(time).format("YYYY-MM-DD");
|
|
|
|
|
}
|
|
|
|
|
function getTheFutureDay(num = 1) {
|
|
|
|
|
const pastFifteenDays = common_vendor.dayjs().add(num, "day");
|
|
|
|
|
return pastFifteenDays.format("YYYY-MM-DD");
|
|
|
|
|
}
|
|
|
|
|
function getCurrentHour() {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const currentHour = now.getHours();
|
|
|
|
|
const currentMinute = now.getMinutes();
|
|
|
|
|
console.log(currentHour, currentMinute, "dayjs().hour()");
|
|
|
|
|
return { currentHour, currentMinute };
|
|
|
|
|
}
|
|
|
|
|
function generateTimeSlots(externalDate = null) {
|
|
|
|
|
const baseDate = externalDate ? common_vendor.dayjs(externalDate) : common_vendor.dayjs();
|
|
|
|
|
const now = externalDate ? common_vendor.dayjs(externalDate) : common_vendor.dayjs();
|
|
|
|
|
const startHour = 8;
|
|
|
|
|
const endHour = 18;
|
|
|
|
|
const interval = 2;
|
|
|
|
|
const currentTime = now.valueOf();
|
|
|
|
|
const slots = [];
|
|
|
|
|
let currentStart = startHour;
|
|
|
|
|
while (currentStart < endHour) {
|
|
|
|
|
const currentEnd = Math.min(currentStart + interval, endHour);
|
|
|
|
|
const startTime = baseDate.hour(currentStart).minute(0).second(0);
|
|
|
|
|
const endTime = baseDate.hour(currentEnd).minute(0).second(0);
|
|
|
|
|
const startStr = startTime.format("HH:mm");
|
|
|
|
|
const endStr = endTime.format("HH:mm");
|
|
|
|
|
const isCurrentPeriod = currentTime >= startTime.valueOf() && currentTime < endTime.valueOf();
|
|
|
|
|
if (endTime.valueOf() > currentTime) {
|
|
|
|
|
slots.push({
|
|
|
|
|
label: isCurrentPeriod ? "两个小时内" : `${startStr}-${endStr}`,
|
|
|
|
|
str: startStr,
|
|
|
|
|
end: endStr,
|
|
|
|
|
isCurrent: isCurrentPeriod,
|
|
|
|
|
date: baseDate.format("YYYY-MM-DD"),
|
|
|
|
|
startTimestamp: startTime.valueOf(),
|
|
|
|
|
endTimestamp: endTime.valueOf()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
currentStart = currentEnd;
|
|
|
|
|
}
|
|
|
|
|
console.log("slots", slots);
|
|
|
|
|
return slots;
|
|
|
|
|
}
|
|
|
|
|
function getTimeRangeDisplay(startTime, endTime, currentTime = null) {
|
|
|
|
|
try {
|
|
|
|
|
const start = common_vendor.dayjs(startTime);
|
|
|
|
|
const end = common_vendor.dayjs(endTime);
|
|
|
|
|
const now = currentTime ? common_vendor.dayjs(currentTime) : common_vendor.dayjs();
|
|
|
|
|
if (!start.isValid() || !end.isValid()) {
|
|
|
|
|
return "时间格式错误";
|
|
|
|
|
}
|
|
|
|
|
if (start.isAfter(end)) {
|
|
|
|
|
return "时间顺序错误";
|
|
|
|
|
}
|
|
|
|
|
if (now.isAfter(start) && now.isBefore(end)) {
|
|
|
|
|
return "两个小时内";
|
|
|
|
|
}
|
|
|
|
|
const isSameDay2 = start.isSame(end, "day");
|
|
|
|
|
if (isSameDay2) {
|
|
|
|
|
return `${start.format("YYYY-MM-DD")} ${start.format("HH:mm")}-${end.format("HH:mm")}`;
|
|
|
|
|
} else {
|
|
|
|
|
return `${start.format("YYYY-MM-DD HH:mm")} - ${end.format("YYYY-MM-DD HH:mm")}`;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return "时间格式错误";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function getFromNow(time) {
|
|
|
|
|
return time ? common_vendor.dayjs(time).fromNow() : "";
|
|
|
|
|
}
|
|
|
|
|
function getIsInRangeInclusive(start, end) {
|
|
|
|
|
const now = common_vendor.dayjs();
|
|
|
|
|
const startDate = common_vendor.dayjs(start);
|
|
|
|
|
const endDate = common_vendor.dayjs(end);
|
|
|
|
|
return now.isAfter(startDate) && now.isBefore(endDate);
|
|
|
|
|
}
|
|
|
|
|
function getDate(time) {
|
|
|
|
|
return time ? common_vendor.dayjs(time).format("YYYY-MM-DD") : "";
|
|
|
|
|
}
|
|
|
|
|
exports.generateTimeSlots = generateTimeSlots;
|
|
|
|
|
exports.getCurrentHour = getCurrentHour;
|
|
|
|
|
exports.getDate = getDate;
|
|
|
|
|
exports.getFromNow = getFromNow;
|
|
|
|
|
exports.getIsInRangeInclusive = getIsInRangeInclusive;
|
|
|
|
|
exports.getTheFutureDay = getTheFutureDay;
|
|
|
|
|
exports.getTimeRangeDisplay = getTimeRangeDisplay;
|
|
|
|
|
exports.showDay = showDay;
|