Difference between two dates

Afternoon

I need to workout the difference between two dates. Although i have two twists to the calculation.

Always a Date (dd/mm/yyyy hh:mm:ss) in A1,
If there is a date in B1 (dd/mm/yyyy hh:mm:ss), then calculate the difference between the two.
If not then calculate the difference between Now() and A1.
This is the formula I have used and works.
IF(ISBLANK(A1),"",(IF(ISBLANK(B1),(NOW()-A1),(B1-A1))

Although I need to take into account to calculate only working days (mon-Fri) which I am afraid is beyond me.

It would be nice to have it stored in the VBA code although if it can only be done as a formula then it will do.

please could you assist in finding me a solution.

Thanks in advance.
Q_evansAsked:
Who is Participating?
 
barry houdiniConnect With a Mentor Commented:
Hello Q evans,

Using your example I get zero days with that formula. One possible issue is that dd:hh:mm format isn't really designed for time durations - the dd part is actually a day of the month (which is why it doesn't work beyond 31 days as I explained above). Another issue is whether you have "1904 date system" set - if you do then you'd get an extra day as you described (because the date system starts counting on day 1 rather than day 0)

If you revert to the default 1900 date format then you won't have that problem. To do that in Excel 2003 or earlier versions:

Tools > Options > Calculation > untick "1904 date system"

or in Excel 2007 and later versions

Click on "Office" button in top left and then "Excel Options" (or in 2010 "File" then "Options") then under "Advanced" go to "When calculating this workbook" and untick "1904 date system".

Having said that, I think the formula is flawed anyway, it won't give the correct results if the B1 (or now) time is earlier in the day than the A1 time (although that doesn't explain the issue you have with 1 day instead of zero). Change to this version for more accurate results:

=NETWORKDAYS(A1,IF(B1="",NOW(),B1))-1+MOD(IF(B1="",NOW(),B1),1)-MOD(A1,1)

As I alluded to in my previous reply, that formula still won't work correctly if any of the times are at the weekend, if that's a possibility change to this version:

=NETWORKDAYS(A1,IF(B1="",NOW(),B1))+NETWORKDAYS(IF(B1="",NOW(),B1),IF(B1="",NOW(),B1))*(MOD(IF(B1="",NOW(),B1),1)-1)-NETWORKDAYS(A1,A1)*MOD(A1,1)

Finally, note that if you change from 1904 date system as suggested then any existing dates in your spreadsheet will change....so if you don't want to change that then I think you need to look at a formula that would return a result like "35 days 13:45" or just show hours and minutes.

If this doesn't answer your question then can you please supply the following information to cut down on the guesswork:

Which Excel version are you using?
Are you knowingly using 1904 date system - is there a reason for that?
What's the longest possible period that you might have between A1 and B1 (or A1 and now)?
Do you want to allow any of the dates to be at weekends?

regards, barry
0
 
nutschCommented:
If you install the analysis toolpak (tools \ add-ins) you can use the networkdays formula, as in

=IF(ISBLANK(A1),"",(IF(ISBLANK(B1),NETWORKDAYS(A1,NOW()),NETWORKDAYS(A1,B1))))

Thomas
0
 
barry houdiniCommented:
Perhaps try this for a shorter formula. NETWORKDAYS counts both start and end date so I assumed you'd want to subtract 1......

=IF(A1="","",NETWORKDAYS(A1,IF(B1="",TODAY(),B1))-1)

regards, barry
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Q_evansAuthor Commented:
Afternoon

Thanks guys but this is not giving me the result I require as it doesnt give me to the nearest minute. It rounds it off to days, I think this it attributed to the NetworkDays function as I have tried this already and come to the same problem.

any other ideas?
0
 
nutschCommented:
try separating the time calculation and the day calculation,on barry's formula

=IF(A1="","",NETWORKDAYS(A1,IF(B1="",TODAY(),B1))-1)+mod(IF(B1="",TODAY(),B1)-a1,1)
0
 
Q_evansAuthor Commented:
Hi guys

That now gives me to nearest hour, I really do need to the nearest minute.

you guys are a great help.
0
 
Q_evansAuthor Commented:
working further with the formula, If B1 is blank, Then it appears to lose a day? but calculates to the minute.
0
 
byundtCommented:
If you plug NOW into Thomas' formula, does that give you the results you want?
=NETWORKDAYS(A1,IF(B1="",NOW(),B1))-1+MOD(IF(B1="",NOW(),B1)-A1,1)

I formatted the results as d hh:mm
0
 
barry houdiniCommented:
I assume that you want to count 24 hours a day on Mondays to Fridays, e.g. 17:00 on Friday to 07:00 on the following Monday would give you just 14:00 hours (17:00 to Midnight on Friday and then midnight to 07:00 on Monday).

That's what Brad's formula will give you, but two observations:

d:hh:mm format is only good for time periods up to 31 days - if you have periods greater than that then you'd to format as [h]:mm for total hours or alter the formula to the following

=NETWORKDAYS(A1,IF(B1="",NOW(),B1))-1-(MOD(IF(B1="",NOW(),B1)-A1,1)<0)&" days "&TEXT(IF(B1="",NOW(),B1)-A1,"h:mm")

which will give you a result like "35 days 13:45"

If either start date, end date (or "now" if end date is blank) might be at the weekend then you'd need a more complex formula......

regards, barry
0
 
Q_evansAuthor Commented:
Thanks, This question is still unaswered,

I have been using this formula
 =NETWORKDAYS(A1,IF(B1="",NOW(),B1))-1+MOD(IF(B1="",NOW(),B1)-A1,1)
Although once the calculation between 1 day and two days seems to falter, eg

18/05/2011 17:51:53 from Now (which at the time was 18/05/2011 22:52:00 give me the answer of 01:05:01 (dd:HH:mm), where is gains an extra day from I am not sure?
0
 
TracyVBA DeveloperCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.