Formula to Calculate Time Worked in Excel

Excel Time Worked Calculator

Use this calculator to quickly sum up time worked across multiple shifts, similar to how you would calculate time differences in Excel. Enter start and end times in HH:MM format (e.g., 09:00 for 9 AM, 17:30 for 5:30 PM). The calculator automatically handles overnight shifts.

Total Time Worked:

Understanding Time Calculations in Excel

Calculating time worked is a common task, especially for payroll, project tracking, or personal time management. While this calculator provides a quick solution, understanding how Excel handles time can be very beneficial.

How Excel Stores Time

Excel stores time as a fraction of a 24-hour day. For example, 6:00 AM is stored as 0.25 (1/4 of a day), 12:00 PM as 0.5 (1/2 of a day), and 6:00 PM as 0.75 (3/4 of a day). A full day is represented by 1. This internal representation is crucial for performing calculations.

Basic Time Difference Formula

To calculate the difference between an end time (e.g., in cell B2) and a start time (e.g., in cell A2) on the same day, you simply subtract:

=B2-A2

If the result appears as a decimal, you need to format the cell to a time format (e.g., [h]:mm or h:mm).

Handling Overnight Shifts

A common challenge arises when a shift crosses midnight (e.g., starting at 10:00 PM and ending at 6:00 AM the next day). If you use the simple subtraction =B2-A2, Excel will return a negative value or a series of hash marks (#######) because the end time is numerically smaller than the start time. To correctly calculate overnight shifts, you need to add 1 (representing one full day) to the end time if it's earlier than the start time:

=(B2<A2)+B2-A2

The (B2<A2) part evaluates to TRUE (which Excel treats as 1) if B2 is less than A2, and FALSE (which Excel treats as 0) otherwise. This effectively adds 24 hours to the end time if it's on the next day.

Summing Total Time

Once you have the duration for each shift, you can sum them up using the SUM function. However, if your total time exceeds 24 hours, Excel's default time format (h:mm) will reset after 24 hours (e.g., 25 hours will display as 1:00). To display total hours beyond 24, you must use a custom number format:

[h]:mm

The square brackets around h tell Excel to display the total number of hours, even if it's greater than 23.

Example Calculation

Let's say you have the following shifts:

  • Shift 1: Start 09:00, End 17:00
  • Shift 2: Start 22:00, End 06:00 (next day)

Using the formula =(B2<A2)+B2-A2:

  • Shift 1: 17:00 – 09:00 = 8:00 hours
  • Shift 2: (TRUE)+06:00 – 22:00 = 1 day + 06:00 – 22:00 = 30:00 – 22:00 = 8:00 hours

Total time worked: 8:00 + 8:00 = 16:00 hours. This calculator performs these exact steps for you.

.calculator-container { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f9f9f9; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); max-width: 700px; margin: 20px auto; color: #333; } .calculator-container h2 { color: #0056b3; text-align: center; margin-bottom: 20px; } .calculator-container p { margin-bottom: 15px; line-height: 1.6; } .calculator-form .input-group { margin-bottom: 15px; display: flex; flex-direction: column; } .calculator-form label { margin-bottom: 5px; font-weight: bold; color: #555; } .calculator-form input[type="text"] { padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; width: 100%; box-sizing: border-box; } .calculator-form button { background-color: #28a745; color: white; padding: 12px 20px; border: none; border-radius: 4px; font-size: 18px; cursor: pointer; transition: background-color 0.3s ease; width: 100%; box-sizing: border-box; margin-top: 10px; } .calculator-form button:hover { background-color: #218838; } .result-container { margin-top: 25px; padding: 15px; background-color: #e9f7ef; border: 1px solid #d4edda; border-radius: 4px; text-align: center; } .result-container h3 { color: #28a745; margin-top: 0; margin-bottom: 10px; } .calculator-result { font-size: 24px; font-weight: bold; color: #0056b3; } .calculator-article { margin-top: 30px; padding-top: 20px; border-top: 1px solid #eee; } .calculator-article h2 { color: #0056b3; margin-bottom: 15px; text-align: left; } .calculator-article h3 { color: #0056b3; margin-top: 20px; margin-bottom: 10px; text-align: left; } .calculator-article ul { list-style-type: disc; margin-left: 20px; margin-bottom: 15px; } .calculator-article li { margin-bottom: 5px; } .calculator-article code { background-color: #e0e0e0; padding: 2px 4px; border-radius: 3px; font-family: 'Courier New', Courier, monospace; font-size: 0.9em; } function parseTime(timeStr) { if (!timeStr) { return NaN; } var parts = timeStr.split(':'); if (parts.length !== 2) { return NaN; } var hours = parseInt(parts[0], 10); var minutes = parseInt(parts[1], 10); if (isNaN(hours) || isNaN(minutes) || hours 23 || minutes 59) { return NaN; } return hours * 60 + minutes; } function calculateTotalTime() { var totalMinutesWorked = 0; var hasValidInput = false; var errorMessages = []; for (var i = 1; i <= 3; i++) { var startTimeStr = document.getElementById('startTime' + i).value.trim(); var endTimeStr = document.getElementById('endTime' + i).value.trim(); if (!startTimeStr && !endTimeStr) { // Skip empty rows continue; } var startTimeMinutes = parseTime(startTimeStr); var endTimeMinutes = parseTime(endTimeStr); if (isNaN(startTimeMinutes) || isNaN(endTimeMinutes)) { errorMessages.push('Invalid time format for Shift ' + i + '. Please use HH:MM (e.g., 09:00).'); continue; // Skip this row but continue processing others } hasValidInput = true; var durationMinutes = endTimeMinutes – startTimeMinutes; // Handle overnight shift (end time is numerically smaller than start time) if (durationMinutes 0) { resultDiv.innerHTML = '' + errorMessages.join(") + ''; return; } if (!hasValidInput) { resultDiv.innerHTML = 'Please enter at least one valid shift time.'; return; } var totalHours = Math.floor(totalMinutesWorked / 60); var remainingMinutes = totalMinutesWorked % 60; resultDiv.innerHTML = totalHours + ' hours and ' + remainingMinutes + ' minutes'; }

Leave a Reply

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