Let Excel do the math for you! ...use the TEXT
function.
The DATEDIF
function (similar to VBA's DateDiff
function) is a quick way to calculate whole years, months, days, hours, and more.
It's ideal that you're storing the value as "number of days" (something that should be done whenever possible, since Excel stores it's dates the same way.
The TEXT
worksheet function (or the FORMAT
function in VBA) can be used to format durations and timespans in the same way it they can format dates and times.
For example:
If cell A1
contains the value you want format, like 12345
, (for 12,345 days), you could use:
=TEXT(A1,"y \yr, M \m, d \d")
...which would return:
33 yr, 10 m, 18 d
Specify placement of date/times values with Y,M,D,H,M,S
and more. You can specify whatever you want between the numbers; if the letter you want are one of the ones listed above, you can place a \
before to force the letter to display (like the d
in the above example.
If your value in A1
included a partial day, like 12345.6789
, you could convert it with:
=TEXT(A1,"y \yr, M \m, d \d, h \hr, m \m, s \s")
....to return:
33 yr, 10 m, 18 d, 16 hr, 17 m, 37 s
Undocumented Worksheet Function: DATEDIF
There is an undocumented worksheet function called DATEDIF
(similar to VBA's DATEDIFF
function) which makes quick work of calculating difference between year, months, days, hours, and more...
"DATEDIF is treated as the drunk cousin of the Formula family. Excel knows it lives a happy and useful life, but will not speak of it in polite conversation." — Chip Pearson
Usage:
The syntax for DATEDIF is as follows:
=DATEDIF(
Date1,
Date2,
Interval
)
Where:
- Date1
is the first date.
- Date2
is the second date.
- Interval
is the interval type to return.
Code Meaning Complete 'calendar' days/months/years
---- ------------------------------- --------------------------------------------------------------
m Months Months between the dates
d Days Days between the dates
y Years Years between the dates
ym Months Excluding Years Months between the dates as if they were of the same year
yd Days Excluding Years Days between the dates as if they were of the same year
md Days Excluding Years & Months Days between the dates as if they were of the same month & year