首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Moment js检查可用用户的开始和结束轮班时间?

使用Moment js检查可用用户的开始和结束轮班时间?
EN

Stack Overflow用户
提问于 2020-06-27 20:39:43
回答 1查看 52关注 0票数 0

我正在尝试通过将当前可用用户的开始和结束轮班时间与当前时间进行比较来创建一个包含当前可用用户的数组。问题是它们是24小时工作的,而我似乎不知道如何编写代码。

下面是可能的时间

代码语言:javascript
复制
 times = {
        "9-18": {
            start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("18:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "12-21": {
            start: moment("12:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "10-14": {
            start: moment("10:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("14:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "9-16": {
            start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("16:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "15-21": {
            start: moment("15:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "21-1": {
            start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("1:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "21-6": {
            start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("6:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        "00-9": {
            start: moment("00:00:00", "HH:mm:ss").format("hh:mm A"),
            end: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
        },
        DO: { start: "DO", end: "DO" },
        CO: { start: "CO", end: "CO" },
    };

然后我有这个代码来比较时间

代码语言:javascript
复制
const currMonthName = moment().format("MMMM YYYY");
const currTime = moment().format("X");
let placeHolder;
const date = new Date();
date.setHours(0, 0, 0, 0);
const today = date
    .toString()
    .split("(Eastern European Summer Time)")[0]
    .trim();
const dayIndex = moment().format("DD") - 1;


const schedule = await Schedule.find();
const availableUsers = [];

for (let user of userNames) {
    let shiftStart = schedule[0][user][currMonthName][dayIndex][today].start;
    let shiftEnd = schedule[0][user][currMonthName][dayIndex][today].end;


    if (
        shiftEnd === moment("6:00:00", "HH:mm:ss").format("hh:mm A") ||
        shiftEnd === moment("1:00:00", "HH:mm:ss").format("hh:mm A") ||
        shiftEnd === moment("9:00:00", "HH:mm:ss").format("hh:mm A")
    ) {

        placeHolder = shiftEnd
        shiftEnd = currTime;

        if (shiftStart <= currTime && shiftEnd >= currTime) {
            availableUsers.push({[user]: {Start: shiftStart, End: placeHolder}});
   

        }
    }
}
res.send(availableUsers);

我知道我这样做是错的,但我不能把我的思想放在心上。

有一个问题是,如果一个人晚上9点开始工作,凌晨1点或更晚才结束,这似乎会把事情搞砸。

EN

回答 1

Stack Overflow用户

发布于 2020-06-28 04:32:22

这段代码可以工作。

代码语言:javascript
复制
isTimeBetween = function (StartTime, EndTime, CurrTime) {
    const currentTime = !CurrTime
        ? moment()
        : moment(CurrTime, "HH:mm a");
    const startTime = moment(StartTime, "HH:mm a");
    const endTime = moment(EndTime, "HH:mm a");

    if (startTime.hour() >= 12 && endTime.hour() <= 12) {
        endTime.add(1, "days");
    }

    const isBetween = currentTime.isBetween(startTime, endTime);

    return isBetween;
};

我说得太早了。这返回false,应该是true...

代码语言:javascript
复制
console.log(isTimeBetween(moment("21:00:00", "HH:mm:ss").format("hh:mm a"), moment("6:00:00", "HH:mm:ss").format("hh:mm a"), moment("00:17:00", "HH:mm:ss").format("hh:mm a")))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62610127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档