排除工作时间的JS中断时间。

huangapple go评论65阅读模式
英文:

JS exclude break timings from working hours

问题

以下是已经翻译好的部分:

I'm trying to split the working time into working slots while excluding Break time.
我试图将工作时间分割成工作时段,同时排除休息时间。

We may have multiple working slots and multiple breaks but time could not overlap.
我们可能有多个工作时段和多个休息时间,但时间不能重叠。

while excluding the break hours and splitting the working time.
在排除休息时间并分割工作时间的同时。

To exclude the time slots Exclusion: True means exclude that time from the Exclusion: false object.
要排除时间时段,Exclusion: True 表示从 Exclusion: false 对象中排除该时间。

example: This is the array of objects with working hours and exclusion hours.
示例:这是包含工作小时和排除小时的对象数组。

And The output I want array of objects with updated working hours to display only working time slots.
我想要的输出是包含已更新的工作小时的对象数组,以仅显示工作时间段。

Then I am converting the time to display on the table like in the below example. This is for just understanding.
然后,我将时间转换为在表格中显示,就像下面的示例一样。这只是为了理解。

These are the working hours.
这是工作时间。
06:00-13:00
14:00-16:00
16:30-18:00

英文:

I'm trying to split the working time into working slots while excluding Break time.
We may have multiple working slots and multiple breaks but time could not overlap.

while excluding the break hours and splitting the working time.
To exclude the time slots Exclusion: True means exclude that time from the Exclusion: false object.

example: This is the array of objects with working hours and exclusion hours.

[
    {
       
        "startHours": "2023-07-13T01:00:44.000Z",
        "finishHours": "2023-07-13T13:00:08.000Z", 
        "exclusion": false
    },
    {
        
       
        "startHours": "2023-07-13T08:00:19.000Z",
        "finishHours": "2023-07-13T09:00:24.000Z",
        "exclusion": true
      
    },
    {
        
        
        "startHours": "2023-07-13T11:00:11.000Z",
        "finishHours": "2023-07-13T11:30:26.000Z",
        "exclusion": true
      
    }
]

And The output I want array of objects with updated working hours to display only working time slots.

[
    {
      
        "startHours": "2023-07-13T01:00:44.000Z",
        "finishHours": "2023-07-13T08:00:19.000Z",
    },
    {
        "startHours": "2023-07-13T09:00:24.000Z",
        "finishHours": "2023-07-13T11:00:26.000Z",
        
    },
    {
        "startHours": "2023-07-13T11:30:26.000Z",
        "finishHours": "2023-07-13T13:00:08.000Z",
        
    }
]

Then I am converting the time to display on the table like in the below example. This is for just understanding.
These are the working hours.

06:00-13:00
14:00-16:00	
16:30-18:00

答案1

得分: 0

以下是您提供的代码的翻译部分:

我提供了带有注释的解释请注意结果可能会根据您的时区而变化您可以指定一个时区以在任何地方获得相同的结果但我不知道是否需要

// 创建一个新的函数以轻松添加小时
Date.prototype.addHours = function(h) {
    this.setHours(this.getHours() + h);
    return this;
}

// 将数组拆分成组的函数
function chunkArrayInGroups(arr, size) {
    var result = [];
    for (var i = 0; i < arr.length; i += size)
        result.push(arr.slice(i, i + size));
    return result;
}

// 这可能因您所在地区而异
const differenceTimezone = 5

// 让我们拆分工作时间和休息时间
var workingHours = input.filter(time => !time.exclusion)
var breaks = input.filter(time => time.exclusion)
var times = []

// 我们想要一天的开始和结束
var start = new Date(workingHours[0].startHours).addHours(differenceTimezone)
var end = new Date(workingHours[0].finishHours).addHours(differenceTimezone)

times.push(start)

// 获取所有休息时间
breaks.forEach(b => {
    var start = new Date(b.startHours).addHours(differenceTimezone)
    var end = new Date(b.finishHours).addHours(differenceTimezone)
    times.push(start, end)
})

times.push(end)

// 最终结果
var result = chunkArrayInGroups(times, 2).map((r) => { return { startHours: r[0], finishHours: r[1] } })

console.log(result)

希望这有助于您理解代码的内容。

英文:

I provided explanation with comments. Bear in mind that the results could very depending on your timezone. You could specify a time zone to get the same results everywhere but I didn't know that was needed.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var input = [
{
&quot;startHours&quot;: &quot;2023-07-13T01:00:44.000Z&quot;,
&quot;finishHours&quot;: &quot;2023-07-13T13:00:08.000Z&quot;, 
&quot;exclusion&quot;: false
},
{
&quot;startHours&quot;: &quot;2023-07-13T08:00:19.000Z&quot;,
&quot;finishHours&quot;: &quot;2023-07-13T09:00:24.000Z&quot;,
&quot;exclusion&quot;: true
},
{
&quot;startHours&quot;: &quot;2023-07-13T11:00:11.000Z&quot;,
&quot;finishHours&quot;: &quot;2023-07-13T11:30:26.000Z&quot;,
&quot;exclusion&quot;: true
}
];
// Create a new function to add hours easily
Date.prototype.addHours= function(h){
this.setHours(this.getHours()+h);
return this;
}
// Function to split up an array into chunks
function chunkArrayInGroups(arr, size) {
var result = [];
for (var i=0; i&lt;arr.length; i+=size)
result.push(arr.slice(i, i+size));
return result;
}
// This could be diffferent in your region
const differenceTimezone = 5
// Let&#39;s split up the working times and breaks
var workingHours = input.filter(time =&gt; !time.exclusion)
var breaks = input.filter(time =&gt; time.exclusion)
var times = []
// We want to start &amp; end of the day
var start = new Date(workingHours[0].startHours).addHours(differenceTimezone)
var end = new Date(workingHours[0].finishHours).addHours(differenceTimezone)
times.push(start)
// Get all the breaks
breaks.forEach(b =&gt; {
var start = new Date(b.startHours).addHours(differenceTimezone)
var end = new Date(b.finishHours).addHours(differenceTimezone)
times.push(start,end)
})
times.push(end)
// The end result
var result = chunkArrayInGroups(times, 2).map((r) =&gt; { return {startHours: r[0], finishHours: r[1]}})
console.log(result)

<!-- end snippet -->

答案2

得分: -1

将字符串转换为日期和反之亦可,以及比较日期,都可以使用 Date 对象来完成,所以我不会详细介绍这个部分。

如果我们从问题中移除这部分内容,问题就变成了从这个数组中找到片段:

const myArray = [
  {
    start: 1,
    end: 100,
    excluded: false,
  },
  {
    start: 6,
    end: 15,
    excluded: true,
  },
  {
    start: 35,
    end: 50,
    excluded: true,
  },
];

整个解决方案可能会比较长,但大致的做法如下:

  • 对所有 startend 值进行排序。在上面的示例中,变成了 1、6、15、35、50、100。
  • 确定系列中的每个片段是否应该被排除。
  • 创建一个新数组来存储这些信息。
  • 合并具有相同 excluded 标志的相邻片段。
  • 最后,移除任何具有 excluded: true 的片段。
英文:

Converting strings to Date and vice versa, and comparing dates are all doable with Date objects, so I won't get into that.

If we remove that from the problem, it comes down to finding the segments out of this array:

const myArray = [
{
start: 1,
end: 100,
excluded: false,
},
{
start: 6,
end: 15,
excluded: true,
},
{
start: 35,
end: 50,
excluded: true,
},
];

The entire solution can be long but this is roughly how you can do it:

  • Sort all start and end values. In the above example, it becomes 1, 6, 15, 35, 50, 100.
  • Determine if each segment in the series should be excluded or not.
  • Create a new array with that info.
  • Merge two adjacent segments with the same excluded flag.
  • Finally, remove any segments with excluded: true.

huangapple
  • 本文由 发表于 2023年7月13日 15:22:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定