2025-06-22 19:41:51 +08:00

60 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import dayjs from 'dayjs';
/**
* 根据当前时间返回对应的时间段
* 返回的时间段是毫秒数
* 例如03:01 - 09:00 返回 120000 (120秒)
* 如果当前时间不在任何时间段内返回0
* @returns
*/
export const getTimeDuration = () => {
// 根据时间返回,返回需要
const timeRangeList = [
{
start: '03:01',
end: '09:00',
duration: 240 * 1000, // 240s
},
{
start: '09:01',
end: '12:00',
duration: 60 * 1000, // 60s
},
{
start: '12:01',
end: '14:00',
duration: 30 * 1000, // 30s
},
{
start: '14:01',
end: '18:00',
duration: 120 * 1000, // 120s
},
{
start: '18:01',
end: '22:00',
duration: 10 * 1000, // 10s
},
{
start: '22:01',
end: '23:59',
duration: 3 * 1000, // 3s
},
{
start: '00:01',
end: '03:00',
duration: 20 * 1000, // 20s
},
];
const currentTime = Date.now();
const currentHour = dayjs(currentTime).format('HH:mm');
for (const range of timeRangeList) {
if (currentHour >= range.start && currentHour <= range.end) {
return range.duration;
}
}
// 如果没有匹配到默认返回0
return 0;
};