【问题标题】:Check available users using their start and end shift time with Moment js?使用 Moment js 的开始和结束轮班时间检查可用用户?
【发布时间】:2020-06-27 12:39:42
【问题描述】:

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

这是可能的时间的样子

 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" },
    };

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

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 点或更晚结束,这似乎会搞砸事情。

【问题讨论】:

    标签: javascript node.js express date momentjs


    【解决方案1】:

    此代码有效。

    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...

    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")))
    

    【讨论】:

      猜你喜欢
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多