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.

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

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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?

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:

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:

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?

=IF(ISBLANK(A1),"",(IF(ISB

Thomas