简体   繁体   English

从一组时间范围(休息)中减去后如何计算总小时数(工作时间)

[英]How to calculate the total hours (working hours) after subtracting from a set of time ranges (break)

I have a form to apply a leave, where you have the option to mention the start date and time , end date and time of the leave applied.我有一个申请休假的表格,您可以在其中选择提及休假的start date and timeend date and time I need to find the total duration in hours and minutes which I have already implemented.我需要找到我已经实施的以小时和分钟为单位的总持续时间。 But there are 2 factors that needs to considered here.但是这里有两个因素需要考虑。

  1. The applied leave should reside within the company working hours.申请的假期应在公司工作时间内。 Example if the user applies leave on 3rd Feb 2022 08:00 AM to 3rd Feb 09:00 PM , and if the company working hours is 09:00 AM to 06:00 PM , the leave duration should be taken from 3rd Feb 2022 09:00 AM to 3rd Feb 06:00 PM例如,如果用户在3rd Feb 2022 08:00 AM3rd Feb 09:00 PM申请休假,如果公司工作时间是09:00 AM to 06:00 PM ,则请假时间应从3rd Feb 2022 09:00 AM to 3rd Feb 06:00 PM开始3rd Feb 2022 09:00 AM to 3rd Feb 06:00 PM

  2. There could be multiple breaks between a day, say a lunch break from 01:00 PM to 02:00 PM , also a break from 04:00 PM to 04:30PM .一天之间可能有多个休息时间,例如从01:00 PM to 02:00 PM的午休时间,以及从04:00 PM to 04:30PM的休息时间。 So this duration should also be deducted from the applied leave if the breaks resides between the applied leave range.因此,如果休息时间位于申请的休假范围之间,也应从申请的休假中扣除此持续时间。

I know this could be done by looping through each time range and checking if it resides between the breaks.我知道这可以通过遍历每个时间范围并检查它是否位于中断之间来完成。 But I'm wondering if there is a better/more elegant way to approach this situation.但我想知道是否有更好/更优雅的方法来处理这种情况。 Any help would be appreciated.任何帮助,将不胜感激。

Instead of thinking of breaks in a day, think of it like blocks of work hours.与其考虑一天的休息时间,不如把它想象成工作时间。 For example, Feb 3rd, 9am to 1pm , Feb 3rd, 2pm to 4pm , Feb 3rd, 4pm to 6pm , Feb 4th, 8am to 11am .例如, Feb 3rd, 9am to 1pmFeb 3rd, 2pm to 4pmFeb 3rd, 4pm to 6pmFeb 4th, 8am to 11am Store your data in this form and you do not need to calculate breaks, but you may need to take 2 partial work blocks from the beginning and end of the leave.以这种形式存储您的数据,您不需要计算休息时间,但您可能需要从休假的开始和结束时抽取 2 个部分工作块。

So you would have some sort of object with all the working hours in chronological order, probably indexed by year and day, with each day having an array of work blocks.因此,您将拥有某种 object,所有工作时间按时间顺序排列,可能按年和日索引,每天都有一系列工作块。 You find where your start time and end time land in the arrays of corresponding days, and possibly calculate partial work blocks.您可以在相应日期的 arrays 中找到您的开始时间和结束时间所在的位置,并可能计算部分工作块。 Then you sum all of the work blocks in between.然后你将所有的工作块相加。

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

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