?
Solved

Difference between two dates

Posted on 2011-03-21
13
Medium Priority
?
274 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:Q_evans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 35182219
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
 
LVL 50

Expert Comment

by:barry houdini
ID: 35182374
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
 

Author Comment

by:Q_evans
ID: 35182718
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
Independent Software Vendors: We Want Your Opinion

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!

 
LVL 39

Expert Comment

by:nutsch
ID: 35182819
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
 

Author Comment

by:Q_evans
ID: 35182891
Hi guys

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

you guys are a great help.
0
 

Author Comment

by:Q_evans
ID: 35182915
working further with the formula, If B1 is blank, Then it appears to lose a day? but calculates to the minute.
0
 
LVL 81

Expert Comment

by:byundt
ID: 35182960
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
 
LVL 50

Expert Comment

by:barry houdini
ID: 35184047
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
 

Author Comment

by:Q_evans
ID: 35798017
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
 
LVL 50

Accepted Solution

by:
barry houdini earned 1000 total points
ID: 35807476
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
 
LVL 24

Expert Comment

by:broomee9
ID: 35877393
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question