日延长 - 瞬间JS

发布于 2025-02-01 02:38:33 字数 1262 浏览 0 评论 0原文

有一项服务在一周的日子开放和关闭(例如:星期一22:00-01:20,星期二:从04:00-13:00,星期三:从17:00-21:00开始。 ..)。目前,我的代码工作,所以假设今天是星期一,时间是23:58 - 服务正在运行。 00:00到01:20没有固定,因为那天已经是星期二和日期发生了变化)。我需要某种标志解决方案,这些解决方案不会切换一天并保持服务打开直到01:20并关闭直到04:00

控制台数据 - starttime -endtime

时间设置

        let startTime = moment(foundScheduleItem.open_at, 'HH:mm')
        let endTime = moment(foundScheduleItem.close_at, 'HH:mm')

        const afterStart = moment().isSameOrAfter(startTime)
        const isToday = afterStart && moment().isSameOrAfter(moment('00:00', 'HH:mm'))
        const beforeClose = moment().isSameOrBefore(endTime)

        if (service.extended) { //  extended - флаг
            const checkTodayBeforeMidnight = isToday && afterStart; 

            // If more than 00:00 but less than close time we dont add the day
            if (!beforeClose && checkTodayBeforeMidnight) {
                endTime = moment('23:59', 'HH:mm'); // no work
             } else {
                startTime = moment('00:00', 'HH:mm');
             }
        }

There is a service that opens and closes on the days of the week (for example: Mon from 22:00 - 01:20, Tue: from 04:00 - 13:00, Wed: from 17:00 - 21:00 ...). At the moment my code works so let's say today is Monday and the time is 23:58 - the service is running .. the time is 00:00 the service is not working and will open at 04:00 .. (the time from 00:00 to 01:20 is not fixed because the day It's already Tuesday and the date is changing). I need some kind of flag solution that will not switch the day and keep the service open until 01:20 and close until 04:00

console data - startTime - endTime

time settings

        let startTime = moment(foundScheduleItem.open_at, 'HH:mm')
        let endTime = moment(foundScheduleItem.close_at, 'HH:mm')

        const afterStart = moment().isSameOrAfter(startTime)
        const isToday = afterStart && moment().isSameOrAfter(moment('00:00', 'HH:mm'))
        const beforeClose = moment().isSameOrBefore(endTime)

        if (service.extended) { //  extended - флаг
            const checkTodayBeforeMidnight = isToday && afterStart; 

            // If more than 00:00 but less than close time we dont add the day
            if (!beforeClose && checkTodayBeforeMidnight) {
                endTime = moment('23:59', 'HH:mm'); // no work
             } else {
                startTime = moment('00:00', 'HH:mm');
             }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文