Excel formula that gives date variance in hours, days or months.

I need an Excel formula that will take two Date/Time fields in an Excel spreadsheet and give me the variance in Hours if the variance is in Hours, variance in Days if the variance is days and variance in Months if the variance is in months.
Variance-Example.xlsx
donnie91910Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may try something like this...

In C2
=IF(AND(DAY(A2)=DAY(B2),MONTH(A2)=MONTH(B2)),TEXT(B2-A2,"h")&" hours",IF(MONTH(A2)=MONTH(B2),DATEDIF(A2,B2,"d")&" Days",DATEDIF(A2,B2,"m")&" Months"))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob HensonFinance AnalystCommented:
Try one of these:

Allows for plural and adds "s"
=IF(B2-A2<1,TEXT((MOD(B2,1)-MOD(A2,1))*24,"0.0")&" Hour"&IF((MOD(B2,1)-MOD(A2,1))*24>1,"s",""),
IF(B2-A2<30,TEXT(DATEDIF(A2,B2,"d"),"0")&" Day"&IF(DATEDIF(A2,B2,"d")>1,"s",""),
TEXT(DATEDIF(A2,B2,"m"),"0")&" Month"&IF(DATEDIF(A2,B2,"m")>1,"s","")))

Open in new window


Just puts (s) in case of plural:
=IF(B2-A2<1,TEXT((MOD(B2,1)-MOD(A2,1))*24,"0.0")&" Hour(s)",
IF(B2-A2<30,TEXT(DATEDIF(A2,B2,"d"),"0")&" Day(s)",
TEXT(DATEDIF(A2,B2,"m"),"0")&" Month(s)"))

Open in new window

0
Rob HensonFinance AnalystCommented:
My suggestion also gives desired result.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
This is the output returned by the two solutions. I am not sure what the other expert tried to prove here.

Variance.jpg
0
Rob HensonFinance AnalystCommented:
Not trying to prove anything, just showing that there are different ways of doing this.

Differences with my solution versus original request:
1) I have allowed for partial hours by showing result to 1 decimal place
2) 2 Options on how to present plurals; result adjusting when plural or just showing (s) whether plural or not.

I would consider both of these improvements on the original requirement.
0
Rob HensonFinance AnalystCommented:
Brackets around the 's or even option for it to be omitted when not required. How can that be seen as not matching the desired output?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
@Donnie

If by any chance, you need to show the time intervals in singulars and plurals both, you may try this...

=IF(MONTH(B2)>MONTH(A2),(DATEDIF(A2,B2,"m")&IF(DATEDIF(A2,B2,"m")=1," Month"," Months")),IF(DAY(A2)=DAY(B2),(TEXT(B2-A2,"h")&IF(TEXT(B2-A2,"h")+0=1," Hour"," Hours")),DATEDIF(A2,B2,"d")&IF(DATEDIF(A2,B2,"d")=1," Day"," Days")))

Open in new window


TimeVariance.jpg
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.