blog.minskio.co.uk

Content and theme behind minskio.co.uk
Log | Files | Refs

decimal-overtime-calculator.md (2122B)


      1 ---
      2 title: "Decimal overtime calculator"
      3 date: 2025-09-05T15:48:00
      4 tags: ["Tools", "Work"]
      5 ---
      6 
      7 My workplace has a quirky rule when it comes to submitting overtime, everything needs to be calculated in decimal hours. While this isn't especially taxing as far as mathmatical problems go I have [built a bash script](https://github.com/breadcat/nix-configs/blob/main/scripts/overtid.nix) to do this, however I'd prefer not to SSH into a server to run a bit of maths, or grab a calculator so here's the same thing in simple JavaScript:
      8 
      9   <label>Start Time: <input type="time" id="startTime" value="07:30"></label>
     10 
     11   <label>End Time: <input type="time" id="endTime" value="19:30"></label>
     12 
     13   <details><summary>Custom cutoffs</summary>
     14     <label>Morning: <input type="time" id="morningCutoff" value="08:30"></label>
     15     <br>
     16     <label>Evening: <input type="time" id="eveningCutoff" value="18:00"></label>
     17   </details>
     18 
     19   <div class="result" id="result"></div>
     20 
     21   <script>
     22     function timeToDecimal(timeStr) {
     23       const [h, m] = timeStr.split(":").map(Number);
     24       return h + m / 60;
     25     }
     26 
     27     function calculate() {
     28       const start = timeToDecimal(document.getElementById("startTime").value);
     29       const end = timeToDecimal(document.getElementById("endTime").value);
     30       const morningCut = timeToDecimal(document.getElementById("morningCutoff").value);
     31       const eveningCut = timeToDecimal(document.getElementById("eveningCutoff").value);
     32 
     33       let before = 0, after = 0;
     34 
     35       if (start < morningCut) {
     36         before = Math.max(0, Math.min(end, morningCut) - start);
     37       }
     38 
     39       if (end > eveningCut) {
     40         after = Math.max(0, end - Math.max(start, eveningCut));
     41       }
     42 
     43       const total = before + after;
     44 
     45       document.getElementById("result").innerHTML = `
     46         <br>
     47         Hours before: ${before.toFixed(2)}. Hours after: ${after.toFixed(2)}<br>
     48         Total hours: ${total.toFixed(2)}
     49       `;
     50     }
     51 
     52     // live update
     53     document.querySelectorAll("input").forEach(input => {
     54       input.addEventListener("input", calculate);
     55     });
     56 
     57     // Run at start
     58     calculate();
     59   </script>