Calculate the exact time duration between two points
Calculation Result:
Total Duration:
Decimal Hours: hours
Total Minutes: minutes
Please enter both a start and end time.
How to Calculate Hours Between Two Times
Whether you are tracking work hours for payroll, managing a study schedule, or timing a long-distance trip, knowing how to calculate the duration between two times is a fundamental skill. This calculator automates the process, accounting for breaks and even overnight shifts.
The Manual Calculation Formula
To calculate hours manually, follow these steps:
Convert both times to 24-hour format: For example, 2:00 PM becomes 14:00.
Convert to total minutes: Multiply the hours by 60 and add the remaining minutes.
Subtract: Subtract the start minutes from the end minutes.
Adjust for cross-midnight: If the result is negative, add 1,440 minutes (the number of minutes in a full day).
Subtract breaks: Deduct any unpaid break time.
Convert back: Divide by 60 to get decimal hours, or find the quotient and remainder for hours and minutes.
Real-World Example
Imagine you start working at 8:45 AM and finish at 5:15 PM, with a 45-minute lunch break.
Start Time: 8:45 (525 minutes from midnight)
End Time: 17:15 (1,035 minutes from midnight)
Difference: 1,035 – 525 = 510 minutes
Subtract Break: 510 – 45 = 465 minutes
Final Result: 7 hours and 45 minutes (or 7.75 decimal hours).
Why Decimal Hours Matter
Payroll systems often require decimal hours rather than hours and minutes. For instance, if you worked 8 hours and 30 minutes, you wouldn't input "8.30" into a payroll system; you would input "8.5" because 30 minutes is half (0.5) of an hour.
function calculateTotalHours() {
var startVal = document.getElementById("startTime").value;
var endVal = document.getElementById("endTime").value;
var breakVal = parseFloat(document.getElementById("breakTime").value) || 0;
var resultsArea = document.getElementById("resultsArea");
var errorArea = document.getElementById("errorArea");
if (!startVal || !endVal) {
errorArea.style.display = "block";
resultsArea.style.display = "none";
return;
}
errorArea.style.display = "none";
// Parse start time
var startParts = startVal.split(":");
var startTotalMinutes = (parseInt(startParts[0]) * 60) + parseInt(startParts[1]);
// Parse end time
var endParts = endVal.split(":");
var endTotalMinutes = (parseInt(endParts[0]) * 60) + parseInt(endParts[1]);
// Handle overnight shifts (end time earlier than start time)
var durationMinutes = endTotalMinutes – startTotalMinutes;
if (durationMinutes < 0) {
durationMinutes += 1440; // Add 24 hours in minutes
}
// Subtract break
var finalMinutes = durationMinutes – breakVal;
if (finalMinutes < 0) {
finalMinutes = 0;
}
// Calculate broken down results
var displayHours = Math.floor(finalMinutes / 60);
var displayMins = Math.round(finalMinutes % 60);
var decimalHours = (finalMinutes / 60).toFixed(2);
// Display results
document.getElementById("formattedResult").innerText = displayHours + "h " + displayMins + "m";
document.getElementById("decimalResult").innerText = decimalHours;
document.getElementById("minutesResult").innerText = Math.round(finalMinutes);
resultsArea.style.display = "block";
}