title: Calculate total duration out of specified time duration Node Js
body: <p>I am calculating Over Time from work start time &amp; work End time.</p>
  <p>We need to calculate Over Time that does not include regular work Timings.</p>
  <p>Example: Regular Work Timings is <code>10:00 AM to 07:00 PM</code></p>
  <p>Over Time need to get from these timings:
  Start Work Hour: <code>13-09-2021 08:00</code>
  End Work Hour: <code>15-09-2021 20:00</code></p>
  <p>We need to calculate total hours outside of <code>10:00 AM to 07:00 PM</code> from Start Work Hour: <code>13-09-2021 08:00</code> &amp; End Work Hour: <code>15-09-2021 20:00</code></p>
  <p>So, here Total OT would be: <code>32 Hour</code></p>
  <p>What I have tried?</p>
  <p>If work Start time and End time in the same day, OT calculation works fine with the below code.</p>
  <p><code>Start Hour : 15-09-21 08:00</code>
  <code>End Hour : 15-09-21 21:00</code></p>
  <pre><code>
  getWorkHours.forEach((workhourOT) =&gt; {
              try {
          
                //  --------------------------------------
                let StartWorkHour = moment(
                  workhourOT.starttime,
                  &quot;DD-MM-YYYY hh:mm:ss&quot;
                );
                let TodayOfficeStartDate =
                  moment(StartWorkHour).format(&quot;DD-MM-YYYY&quot;);
  
                let StartOfficeHour = moment(
                  `${TodayOfficeStartDate}` + ` 10:00:00`,
                  &quot;DD-MM-YYYY hh:mm:ss&quot;
                );
  
                const diffInMillisecondsBeforeOfficeHour = Math.abs(
                  new Date(StartWorkHour) - new Date(StartOfficeHour)
                );
                let OTBeforeOffice =
                  diffInMillisecondsBeforeOfficeHour / 1000 / 3600;
                console.log(OTBeforeOffice);
                let EndtWorkHour = moment(
                  workhourOT.endtime,
                  &quot;DD-MM-YYYY hh:mm:ss&quot;
                );
  
                let TodayOfficeEndDate =
                  moment(EndtWorkHour).format(&quot;DD-MM-YYYY&quot;);
  
                let EndOfficeHour = moment(
                  `${TodayOfficeEndDate}` + ` 19:00:00`,
                  &quot;DD-MM-YYYY hh:mm:ss&quot;
                );
  
                const diffInMillisecondsAfterOfficeHour = Math.abs(
                  new Date(EndtWorkHour) - new Date(EndOfficeHour)
                );
                let OTAfterOffice =
                  diffInMillisecondsAfterOfficeHour / 1000 / 3600;
                console.log(OTAfterOffice);
                let totalWORKOT = OTAfterOffice + OTBeforeOffice;
  
                data[&quot;TotalWork_OT&quot;] = totalWORKOT.toFixed(1);
              } catch (error) {
                console.log(error);
              }
            });
  
  
  </code></pre>
  <p>OutPut is</p>
  <pre><code>{
  
      &quot;TotalWork_OT&quot;: &quot;4.0&quot;,
  
  }
  
  </code></pre>