views:

45

answers:

1

Hi all - I've been trying to get this Excel function working correctly, and I've hit a wall. I'm trying to calculate the exact difference in days between two dates, taking the start time into account as well as the start day. Seems like this should be a common need?

Example:

   Start Date            End Date                 Expected Result
9/20/2010 8:00am      9/22/2010 3:00pm              2.3 days

I'm guessing at the .3, but you get the idea :) My current formula looks like this:

=IF(End < NOW(), 0, IF(Start >= NOW(), End-Start+1, End-NOW()))

It works almost perfectly...the only hiccup is if today is between the start/end dates, it only calculates full days, and does not include partials.

Here are the parameters for this function:

  • Today is before start date: Calculate full number of days
  • Today is between start and end date: Calculate exact days left, taking start/end hours into account
  • Today is start date: Calculate exact days left, taking start/end hours into account
  • Today is start date, hours left: Calculate exact days left, taking start/end hours into account
  • Today is end date, after end time: zero
  • Today is end day, hours left: Calculate exact days left, taking start/end hours into account
  • Today is after end date: zero
A: 

I've just tried this in Excel 2000 and the clause with the problem [End - Now())] does include decimal places in the answer. Have you set the column format to include decimal places in the display?

However, I would change [End-Start+1] to [CEILING(End-Start,1)]. This rounds up the value to the nearest whole number of days. This gives:

=IF(End < NOW(), 0, IF(Start >= NOW(), CEILING(End-Start,1), End-NOW()))
arx
Since he seems to want one decimal place, I think you'd want 0.1 in the CEILING function.
Lance Roberts
The ceiling is for the clause "Today is before start date: Calculate full number of days". I guess he means a whole number and that's why he had the +1 originally (so the display rounding would produce a sometimes sensible looking whole number of days).
arx
arx - The columns are formated to show two decimal places...and they show .00
morganpdx
Just for reference, I have a test case for each of those situations:
morganpdx
Oops hit enter :) Today is before start date Sep 26 12:39p Oct 8 12:39pToday is between start and end date Sep 16 10:15a Sep 28 5:27pToday is start date Sep 21 12:39p Oct 3 12:39pToday is start date, hours left Sep 21 10:15a Oct 3 12:39pToday is end date Sep 16 12:39p Sep 21 12:39pToday is end day, hours left Sep 16 12:39p Sep 21 5:27pToday is after end date Sep 1 12:39p Sep 13 12:39p
morganpdx
Yeah, formatting. Ick. I added the CEILING function, too. The problem is that the formula is not counting partial days, as well as running into that date difference issue where it's always one day short. So in the case where my start date is 9/20/2010 at 8AM, my end date is 9/30/2010 at 5PM, the expected result for total days should be 11 if today is before 9/20/2010 at 8AM, and it the current datetime is 9/25/2010 at 3PM, the remaining days left should be 5.SomeSmallDecimal.
morganpdx
I'm wishing there was a DateTIME difference function, that I could specify the return result was in Days. DateDiff doesn't seem to work either. /headdesk
morganpdx
"start date is 9/20/2010 at 8AM, my end date is 9/30/2010 at 5PM, the expected result for total days should be 11 if today is before 9/20/2010 at 8AM". Yes, I get 11. "current datetime is 9/25/2010 at 3PM, the remaining days left should be 5.SomeSmallDecimal" Yes, I get 5.08. For reference the exact formula is "=IF(B1 < NOW(), 0, IF(A1 >= NOW(), CEILING(B1-A1,1),B1- NOW()))"
arx
Aha! I must've been looking at the wrong formula. That's what I get for juggling so much in my head at once. Thanks arx! I'll take a closer look at that CEILING function.
morganpdx