Work Hours Calculator

Work Hours Calculator

This calculator helps you determine the total number of work hours based on your start and end times, factoring in any breaks. It's useful for freelancers, hourly employees, or anyone who needs to accurately track their working time.

function calculateWorkHours() { var startTimeInput = document.getElementById("startTime").value; var endTimeInput = document.getElementById("endTime").value; var breakDurationInput = document.getElementById("breakDuration").value; var resultDiv = document.getElementById("result"); resultDiv.innerHTML = ""; // Clear previous results // Validate start and end times var startTimeParts = startTimeInput.split(':'); var endTimeParts = endTimeInput.split(':'); if (startTimeParts.length !== 2 || endTimeParts.length !== 2) { resultDiv.innerHTML = "Please enter times in HH:MM format."; return; } var startHour = parseInt(startTimeParts[0], 10); var startMinute = parseInt(startTimeParts[1], 10); var endHour = parseInt(endTimeParts[0], 10); var endMinute = parseInt(endTimeParts[1], 10); if (isNaN(startHour) || isNaN(startMinute) || isNaN(endHour) || isNaN(endMinute)) { resultDiv.innerHTML = "Invalid time format. Please use numbers for hours and minutes."; return; } if (startHour 23 || startMinute 59 || endHour 23 || endMinute 59) { resultDiv.innerHTML = "Hours must be between 00-23 and minutes between 00-59."; return; } // Validate break duration var breakDuration = parseInt(breakDurationInput, 10); if (isNaN(breakDuration) || breakDuration < 0) { resultDiv.innerHTML = "Break duration must be a non-negative number."; return; } // Convert times to minutes from midnight var startTotalMinutes = startHour * 60 + startMinute; var endTotalMinutes = endHour * 60 + endMinute; // Handle overnight shifts if (endTotalMinutes < startTotalMinutes) { endTotalMinutes += 24 * 60; // Add minutes for the next day } var totalWorkMinutes = endTotalMinutes – startTotalMinutes – breakDuration; if (totalWorkMinutes < 0) { resultDiv.innerHTML = "End time must be after start time, considering breaks."; return; } var totalWorkHours = Math.floor(totalWorkMinutes / 60); var remainingMinutes = totalWorkMinutes % 60; resultDiv.innerHTML = "Total Work Hours: " + totalWorkHours + " hours and " + remainingMinutes + " minutes."; } .calculator-container { font-family: sans-serif; border: 1px solid #ccc; padding: 20px; border-radius: 8px; max-width: 400px; margin: 20px auto; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } .calculator-container h2 { text-align: center; margin-bottom: 15px; color: #333; } .calculator-container p { margin-bottom: 20px; color: #555; line-height: 1.5; } .input-section { margin-bottom: 15px; } .input-section label { display: block; margin-bottom: 5px; font-weight: bold; color: #444; } .input-section input[type="text"], .input-section input[type="number"] { width: calc(100% – 20px); padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; } button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } .result-section { margin-top: 20px; padding: 15px; background-color: #e9ecef; border: 1px solid #ced4da; border-radius: 4px; text-align: center; font-weight: bold; color: #333; }

Leave a Reply

Your email address will not be published. Required fields are marked *