2026-01-14 15:43:09 +08:00

78 lines
2.8 KiB
JavaScript

"use strict";
const common_vendor = require("../vendor.js");
common_vendor.dayjs.locale("zh-cn");
common_vendor.dayjs.extend(common_vendor.relativeTime);
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 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") : "";
}
function specificTime(targetTime) {
const now = common_vendor.dayjs();
const target = common_vendor.dayjs(targetTime);
let hoursDiff = Math.abs(target.diff(now, "hour", true)) <= 2 || target.isBefore(now);
console.log(hoursDiff, "hoursDiff");
return hoursDiff;
}
exports.generateTimeSlots = generateTimeSlots;
exports.getCurrentHour = getCurrentHour;
exports.getDate = getDate;
exports.getFromNow = getFromNow;
exports.getIsInRangeInclusive = getIsInRangeInclusive;
exports.getTheFutureDay = getTheFutureDay;
exports.showDay = showDay;
exports.specificTime = specificTime;