简体   繁体   English

如何使用javascript将时间段拆分为每日时间段?

[英]How to split time slots into day wise slots using javascript?

Any help will be really appreciated! 任何帮助将不胜感激! I'm trying from 4 days but still not found any solution. 我从4天开始尝试,但仍未找到任何解决方案。 Please help me. 请帮我。 Guide me for library or methods i can use. 指导我使用的库或方法。

Here is the given input. 这是给定的输入。 array of session start and end. 会话开始和结束的数组。 I want to calculate daily usage period. 我想计算每日使用期限。 So i want to split each slot in their own day. 所以我想在自己的一天中分配每个时间段。

var slots = [
  { start: dayjs('2019-01-01 21:00:00'), end: dayjs('2019-01-01 23:00:00') },
  { start: dayjs('2019-01-01 22:00:00'), end: dayjs('2019-01-02 01:00:00') },
  { start: dayjs('2019-01-01 22:00:00'), end: dayjs('2019-01-02 04:00:00') },
  { start: dayjs('2019-01-01 21:00:00'), end: dayjs('2019-01-02 00:00:00') },
  { start: dayjs('2019-01-02 00:00:00'), end: dayjs('2019-01-02 04:00:00') },
  { start: dayjs('2019-01-02 01:00:00'), end: dayjs('2019-01-02 04:00:00') },
  { start: dayjs('2019-01-31 01:00:00'), end: dayjs('2019-02-01 04:00:00') },
]
var output = []

slots.forEach((slot) => {
  // filter same slots with same day
  if (slot.start.isSame(slot.end, 'day')) {
    output.push(slot)
  } else {
  // what to do here? how to split end time
  }
})
console.log(output)

I need this kind of output 我需要这种输出

[
  { start: '2019-01-01 21:00:00', end: '2019-01-01 23:00:00' },
  { start: '2019-01-01 22:00:00', end: '2019-01-01 23:59:59' },
  { start: '2019-01-02 00:00:00', end: '2019-01-02 01:00:00' },
  { start: '2019-01-01 22:00:00', end: '2019-01-01 23:59:59' },
  { start: '2019-01-02 00:00:00', end: '2019-01-02 04:00:00' },
  { start: '2019-01-01 21:00:00', end: '2019-01-01 23:59:59' },
  { start: '2019-01-02 00:00:00', end: '2019-01-02 00:00:00' },
  { start: '2019-01-02 00:00:00', end: '2019-01-02 04:00:00' },
  { start: '2019-01-02 01:00:00', end: '2019-01-02 04:00:00' },
  { start: '2019-01-31 01:00:00', end: '2019-01-31 23:59:59' },
  { start: '2019-02-01 00:00:00', end: '2019-02-01 04:00:00' },
]

The idea would be to iteratively split a time slot into two, the first going from the start of the time slot to the end of the day of the start time, and the second going from the start of the next day of the start time (add a day) to the end time. 这个想法是将一个时隙迭代地分为两个,第一个从时隙的开始到开始时间的一天结束,第二个从开始时间的第二天开始(添加一天)到结束时间。

{ start: dayjs('2019-01-01T22:00:00-00:00'), end: dayjs('2019-01-02T01:00:00-00:00') }

becomes

[
  { start: dayjs('2019-01-01T22:00:00-00:00'), end: dayjs('2019-01-02T23:59:59-00:00') },
  { start: dayjs('2019-01-02T00:00:00-00:00'), end: dayjs('2019-01-02T01:00:00-00:00') }
]

Note, it appears that dayjs uses full DateTime format by default. 注意,默认情况下, dayjs似乎使用完整的DateTime格式。

Algorithm [Updated with new info about multi-day slots from @asissuthar] 算法[已通过@asissuthar更新了有关多日空档的新信息]

const slotHopper = { ...slot }; // don't mutate original slot
while (!slotHopper.start.isSame(slotHopper.end, "day")) {
  // peel off first day of slot
  const splitSlot = {
    start: slotHopper.start,
    end: dayjs(slotHopper.start).endOf("day")
  };
  acc.push(splitSlot);
  // update start to beginning of next day
  slotHopper.start = dayjs(slotHopper.start)
    .add(1, "day")
    .startOf("day");
}
acc.push(slotHopper);

I extracted the above into a reducer function in the following sandbox example: https://codesandbox.io/s/lp0z5x7zw9 , where acc is the accumulation array for the result. 我在以下沙箱示例中将以上内容提取到了reducer函数中: https : //codesandbox.io/s/lp0z5x7zw9 ,其中acc是结果的累积数组。

You can do the following using reduce . 您可以使用reduce进行以下操作。 I don't know dayjs . 我不知道dayjs I'm assuming you're using it only to compare the date part and it's not part of the original array. 我假设您仅使用它来比较日期部分,而不是原始数组的一部分。 So, instead I have created a function which gives just the date part for start and end . 因此,相反,我创建了一个函数,该函数仅给出了startenddate部分。

 const slots = [ { start: '2019-01-01 21:00:00', end: '2019-01-01 23:00:00' }, { start: '2019-01-01 22:00:00', end: '2019-01-02 01:00:00' }, { start: '2019-01-01 22:00:00', end: '2019-01-02 04:00:00' }, { start: '2019-01-01 21:00:00', end: '2019-01-02 00:00:00' }, { start: '2019-01-02 00:00:00', end: '2019-01-02 04:00:00' }, { start: '2019-01-02 01:00:00', end: '2019-01-02 04:00:00' }, { start: '2019-01-31 01:00:00', end: '2019-02-01 04:00:00' }]; const getDay = (date) => date.split(" ")[0]; const newArray = slots.reduce((acc, {start,end}) => { if (getDay(start) != getDay(end)) acc.push({ start, end: `${getDay(start)} 23:59:59` }, { start: `${getDay(end)} 00:00:00`, end }); else acc.push({ start, end }) return acc }, []); console.log(newArray) 

Finally found solution. 终于找到了解决办法。 it works perfactly. 它完美地工作。 Execute 执行

import dayjs from "dayjs";

const slots = [
  { start: dayjs("2019-01-01 21:00:00"), end: dayjs("2019-01-01 23:00:00") },
  { start: dayjs("2019-01-01 22:00:00"), end: dayjs("2019-01-02 01:00:00") },
  { start: dayjs("2019-01-01 22:00:00"), end: dayjs("2019-01-02 04:00:00") },
  { start: dayjs("2019-01-01 21:00:00"), end: dayjs("2019-01-02 00:00:00") },
  { start: dayjs("2019-01-02 00:00:00"), end: dayjs("2019-01-02 04:00:00") },
  { start: dayjs("2019-01-02 01:00:00"), end: dayjs("2019-01-02 04:00:00") },
  { start: dayjs("2019-01-31 01:00:00"), end: dayjs("2019-02-01 04:00:00") },
  { start: dayjs("2019-02-01 01:00:00"), end: dayjs("2019-02-04 04:00:00") }
];

function splitDayWise(slots) {
  let output = [];

  function pushSlot(slot, start, end) {
    output.push({
      ...slot
    });

    let top = output[output.length - 1];
    top.start = start;
    top.end = end;
    top.time = top.end - top.start;
  }

  slots.forEach(slot => {
    if (slot.start.isSame(slot.end, "day")) {
      pushSlot(slot, slot.start, slot.end);
    } else {
      while (!slot.start.isSame(slot.end, "day")) {
        pushSlot(slot, slot.start, slot.start.endOf("day"));
        slot.start = slot.start.add(1, "day").startOf("day");
      }
      pushSlot(slot, slot.start, slot.end);
    }
  });

  return output;
}

const daywiseSlots = splitDayWise(slots).map(slot => ({
  start: slot.start.format("YYYY-MM-DD HH:mm:ss"),
  end: slot.end.format("YYYY-MM-DD HH:mm:ss"),
  time: slot.time
}));

console.log(JSON.stringify(daywiseSlots, null, 2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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