javascript 需要逻辑过滤数据的基础上当前的时间

xdnvmnnf  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(87)

我有这个对象如下所示,

const user = {
  id: 3,
  name: "Clark Kent",
  mobileNumber: "1234567892",
  email: "[email protected]",
  role: "VAN_SALE",
  loc_update_permission: true,
  active_shifts: [
    {
      id: 1,
      name: "Morning",
      startTime: "09:30:00",
      endTime: "05:30:00",
      coordinates: null,
    },
    {
      id: 2,
      name: "Night",
      startTime: "06:30:00",
      endTime: "02:30:00",
      coordinates: null,
    },
  ],
};

我想做的是根据当前时间获取当前活动的班次。例如,如果当前时间在09:30:0005:30:00之间或等于05:30:00,则它应该返回Morning作为当前活动的偏移,反之亦然(我将开始和结束时间存储为time在我的数据库中)。

function getCurrentActiveShift(user) {
  const currentTime = new Date();
  const currentHours = currentTime.getHours() * 60 + currentTime.getMinutes(); // Convert to minutes

  const shifts = user.active_shifts;

  console.log(currentTime);
  console.log(currentTime.toLocaleString());

  console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);

  for (const shift of shifts) {
    const startParts = shift.startTime.split(":");
    const startHours = parseInt(startParts[0]) * 60 + parseInt(startParts[1]); // Convert to minutes

    const endParts = shift.endTime.split(":");
    const endHours = parseInt(endParts[0]) * 60 + parseInt(endParts[1]); // Convert to minutes

    // Handle shifts that span midnight
    if (endHours < startHours) {
      if (currentHours >= startHours || currentHours < endHours) {
        return shift;
      }
    } else {
      if (currentHours >= startHours && currentHours < endHours) {
        return shift;
      }
    }
  }

  return null; // No active shift found for the current time
}

const user = {
  id: 3,
  name: "Clark Kent",
  mobileNumber: "1234567892",
  email: "[email protected]",
  role: "VAN_SALE",
  loc_update_permission: true,
  active_shifts: [
    {
      id: 1,
      name: "Morning",
      startTime: "09:30:00",
      endTime: "05:30:00",
      coordinates: null,
    },
    {
      id: 2,
      name: "Night",
      startTime: "06:30:00",
      endTime: "02:30:00",
      coordinates: null,
    },
  ],
};

const currentShift = getCurrentActiveShift(user);
if (currentShift) {
  console.log(`Current active shift: ${currentShift.name}`);
} else {
  console.log("No active shift at the current time.");
}

这就是我目前为止所做的,但是为了测试,我在当前时间上增加了10个小时,比如currentTime.setHours(currentTime.getHours() + 10);,所以它应该返回Night作为当前活动的偏移,但是它仍然返回Morning
谁能帮帮我我是否需要改变我在db中存储shift时间的方式?
主要的是有来自2个不同时区的人使用此逻辑,因此如果来自IST和AST时区的用户获得此数据,如果他们当前的时间是上午9:00,则他们当前的活动班次都应显示上午。

rqdpfwrv

rqdpfwrv1#

注:逻辑上的问题是错误的,我们如何将时间是否在24小时或12小时格式?
我尝试了下面的解决方案。我想这解决了问题。

const user = {
  id: 3,
  name: "Clark Kent",
  mobileNumber: "1234567892",
  email: "[email protected]",
  role: "VAN_SALE",
  loc_update_permission: true,
  active_shifts: [
    {
      id: 1,
      name: "Morning",
      startTime: "09:30:00",
      endTime: "05:30:00",
      coordinates: null,
    },
    {
      id: 2,
      name: "Night",
      startTime: "06:30:00",
      endTime: "02:30:00",
      coordinates: null,
    },
  ],
};

function getCurrentActiveShift(user) {
  const currentTime = new Date();
  let hour = currentTime.getHours()
  if(hour>12) hour -= 12;
  const currentTimeStr = `${hour}${currentTime.getMinutes()}${currentTime.getSeconds()}`
  const shifts = user.active_shifts;
  for (const shift of shifts) {
    const {startTime, endTime} = shift;
    const [startTimeStr, endTimeStr] = [startTime.replaceAll(":", ""), endTime.replaceAll(":", "")]
    if(Math.min(startTimeStr, endTimeStr) <= currentTimeStr &&currentTimeStr<=  Math.max(startTimeStr, endTimeStr))
    return shift.name
    
  }
  return 'No Active shift'; // No active shift found for the current time
}

console.log(`Current active shift: ${getCurrentActiveShift(user)}`);

相关问题