zaro

How to calculate time difference in Excel in 24 hours format?

Published in Excel Time Calculation 3 mins read

To calculate the time difference in Excel in 24 hours format, you can subtract the start time from the end time. Excel treats dates and times as serial numbers, making arithmetic operations straightforward. The "24 hours format" in this context usually refers to displaying the result as a total number of hours (e.g., 9.5 hours for 9 hours and 30 minutes) or as a duration that can exceed 24 hours (e.g., 30:00 for 30 hours).

Calculating Total Hours as a Number

The most direct way to get the time difference as a numerical value representing total hours is to subtract the start time from the end time and then multiply the result by 24. This method is ideal for calculating total work hours, project durations, or any scenario where you need a decimal value of hours.

Step-by-Step Guide:

  1. Enter Start and End Times:

    • In cell A2, enter your Start Time (e.g., 8:00 AM or 08:00).
    • In cell B2, enter your End Time (e.g., 5:00 PM or 17:00).
    • Tip: If A2 already has the desired time format, you can use the Format Painter (Home tab > Format Painter) to quickly apply the same formatting to B2. Just select A2, click Format Painter, and then click B2.
  2. Apply the Formula:

    • In cell C2, enter the following formula:
      =(B2-A2)*24
    • Press Enter.
  3. Interpret the Result:

    • Excel will display the total time difference in hours as a decimal number. For example, if A2 is 8:00 AM and B2 is 5:00 PM, the result in C2 will be 9. If the end time was 5:30 PM, the result would be 9.5.
    • Ensure cell C2 is formatted as 'General' or 'Number' to display the decimal value correctly. You can adjust the number of decimal places as needed.

Displaying Time Difference as a Duration ([h]:mm Format)

If you need the time difference to display as a duration in hours and minutes, even if it's more than 24 hours (e.g., 30:15 for 30 hours and 15 minutes), you'll use a custom number format.

Step-by-Step Guide:

  1. Enter Start and End Times:

    • Enter your Start Time in cell A2 (e.g., 10/26/2023 8:00 AM).
    • Enter your End Time in cell B2 (e.g., 10/27/2023 2:00 PM).
    • Note: Including dates is crucial if your time difference spans midnight or multiple days.
  2. Apply the Basic Formula:

    • In cell C2, enter the formula:
      =B2-A2
    • Press Enter.
  3. Apply Custom Number Format:

    • Select cell C2.
    • Right-click and choose Format Cells... (or press Ctrl + 1 on Windows, Cmd + 1 on Mac).
    • In the Format Cells dialog box, go to the Number tab, then select Custom from the category list.
    • In the "Type:" field, enter:
      • For seconds, use [h]:mm:ss.
    • Click OK.
  4. Interpret the Result:

    • The cell will now display the total duration in hours and minutes. For example, if the start time was 8:00 AM on day 1 and the end time was 2:00 PM on day 2, the result would be 30:00.

Handling Time Differences Spanning Midnight

When calculating time differences where the end time is on the next day (e.g., starting at 10:00 PM and ending at 2:00 AM), a simple subtraction will result in a negative number or an incorrect value. You need to adjust the formula by adding 1 (representing one day) to the end time if it's earlier than the start time.

Formula for Times Spanning Midnight:

=IF(B2<A2,B2+1-A2,B2-A2)
  • A2: Start Time (e.g., 10:00 PM)
  • B2: End Time (e.g., 2:00 AM)

After applying this formula, you can then:

  • Multiply by 24 to get the total hours as a number:
    =IF(B2<A2,B2+1-A2,B2-A2)*24
  • Apply the [h]:mm custom format to display it as a duration:
    =IF(B2<A2,B2+1-A2,B2-A2)

    Then format the cell as [h]:mm.

Practical Examples and Solutions

Here's a table summarizing common scenarios and their solutions:

Scenario Start Time (A2) End Time (B2) Formula for Duration [h]:mm Result ([h]:mm) Formula for Total Hours *24 Result (General) Notes
Same Day 9:00 AM 5:00 PM =B2-A2 8:00 =(B2-A2)*24 8 Simplest case.
Spanning Midnight 10:00 PM 2:00 AM =IF(B2<A2,B2+1-A2,B2-A2) 4:00 =IF(B2<A2,B2+1-A2,B2-A2)*24 4 Accounts for crossing to next day.
Multi-Day Duration 10/26/23 8:00 AM 10/28/23 4:00 PM =B2-A2 56:00 =(B2-A2)*24 56 Requires dates for accurate calculation.
Time with Minutes 9:15 AM 5:45 PM =B2-A2 8:30 =(B2-A2)*24 8.5 Excel handles minutes automatically.

Best Practices for Time Calculations

  • Include Dates for Accuracy: If your time differences can span midnight or multiple days, always include the full date and time (e.g., 10/26/2023 8:00 AM) to ensure Excel calculates correctly.
  • Use Custom Formats for Durations: For displaying time durations that might exceed 24 hours, always use custom formats like [h]:mm or [h]:mm:ss. This prevents Excel from wrapping the time back around 24 hours (e.g., showing 2 hours instead of 26 hours).
  • Format Cells Correctly: Ensure the cells containing your time inputs are formatted as "Time" or "Date" and your results are formatted appropriately ("General" or "Number" for total hours, "Custom" for durations).
  • Understand Excel's Time Values: Remember that Excel stores time as a fraction of a 24-hour day. 12:00 PM is 0.5, 6:00 AM is 0.25, etc. This understanding helps in troubleshooting formula results.
  • For more detailed information, consult the official Microsoft Excel support documentation on calculating time.