简体   繁体   中英

Check available users using their start and end shift time with Moment js?

I am trying to make an array containing the currently available users by comparing their start and end shift times with the current time. The problem is that they work in a 24h schedule and i can't seem to figure out how to write the code.

Here's what the possible times look like

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

Then i have this code to compare times

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

I know i'm doing this all wrong but can't wrap my mind around it.

There's the issue that if someone starts at 9PM and finishes at 1AM or later, it seems to mess things up.

This code works.

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

i spoke too soon. This returns false, should be 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")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM