I had an email from Steve a little while ago asking how he could display the number of elapsed days, hours and minutes in one cell as d h:mm, like this in C5 below:
Notes:
1. my dates are dd/mm/yyyy.
2. In this tutorial I am assuming you're familiar with how Excel uses serial numbers for dates and time. If not click here to learn about dates and time in Excel first.
Ok, now you know all about serial numbers let's look at my formula in cell C5:
=INT(end-start)&" "&TEXT(end-start,"h:mm")
A5 = start
B5 = end
The INT function simply rounds the number down to the nearest integer, or whole number.
In English it reads:
Calculate the whole number of days between the end and start dates (this will give the days figure), & add a space & then calculate the end minus start and display the hours and minutes portion as text formatted as h:mm.
We can see it evaluate in stages like so:
Step 1: = 2&" "&14:00
Step 2: 2 14:00
The limitation of this result is that it is text as opposed to a number, which means it can’t be used in another formula that SUM’s or AVERAGE’s etc., as these functions require numbers.
I have done this because the number of elapsed days could be > 31 and Excel cannot display cumulative days > 31 as well as time in one cell.
If you’re confused about this last statement, keep reading and all will become clearer with the next example….hopefully!
Elapsed Days and Time as a Number
If the number of days between your start and end dates is always <32 days, then you can use a much simpler formula and apply a custom number format to the cell like this in cell D5:
Formulas don’t get much easier than that!
To display the elapsed time as d h:mm I simply used this custom number format:
The Limitation
You can see in the example above the last calculation in cell D8 gives a different result to C8, and this is because the number of days is >31.
In D8 we get 23 days when it should be 54 days. This is because 54 days from the 1st Jan 1900 (the day Excel dates start), takes us up to the 23rd Feb 1900 and Excel is simply displaying the day portion (23) of the underlying serial number Excel uses for dates.
This is what I mean by 'Excel cannot show the cumulative number of days using a custom number format for dates/time in one cell'.
So, if your elapsed time is likely to exceed 31 days you need to use the first option which results in text. If you also need the elapsed time calculated as a number for use in other formulas then you will need to use helper columns to house that calculation.
In case you missed it, here's a link to my tutorial on working with Time in Excel which explains how serial numbers are used to determine dates.
Thanks
Thanks to Steve for asking this great question. It highlights some of the constraints when working with time in Excel.
Vincent Cassidy
Hi,
I’m having a problem conditional formatting time spans.
Col A. lists Planned arrival times, Col B. lists actual arrival times.
if the actual is early or on time show a green.
if the actual is more than an hour late show red.
I’ve tried =B1<=(A1+(1/24)) but it falls down when the time goes either side of midnight.
Can you suggest a solution.
Many thanks
Vince
Mynda Treacy
Hi Vincent,
That’s a tricky one. I think the solution is to put the date and the time in the cell. e.g.:
Planned time 1/1/2013 1:00 AM
Actual time 31/12/2012 11:00 PM
You can format the cell to only display the time, but you need to enter the date and time so that Excel can accurately compare the actual vs planned times using the full serial number.
More on dates and times and serial numbers in Excel here.
I hope that helps.
Kind regards,
Mynda.
Vincent Cassidy
Thankyou for the prompt reply Mynda,
That’s exactly what I did in the end. As I will have users entering the times I was just hoping for some miracle solution that would make entering the data less messy.
Regards
Vince
Mynda Treacy
I know what you mean by making it less messy for data entry.
The problem is the logic in your test is not black and white, unlike calculating times for timesheets that straddle 2 dates, where if the start of the shift is say 23:00 and the end of the shift is the following day at 6:00 AM then you can test if the start is > than the end and apply a solution as shown at that link.
However sometimes this scenario will result in your arrival time being early, and sometimes it will be late, it’s not one or the other, it could be both.
Cheers,
Mynda.