0

I am trying to make an algorithm that counts the days between two dates, e.g. between 3/1/2012 and 3/2/2012.

What is the correct answer, or the most popular choice? Should be the one I use?

In this case, if I don't include both dates I am comparing, the days are 0; if I include one of them, the days are 1; if I include both, the days are 2.

5
  • 6
    Tomorrow is 1 day away. Always. I just checked the API docs
    – user7071
    Commented May 31, 2012 at 2:54
  • 4
    If Little Orphan Annie says it, it must be true. Commented May 31, 2012 at 3:08
  • 1
    Ha ha, everything is copyrighted, damn. But wait, I just googled it, and there are 2 exceptions. There is no tomorrow after certain Last Day for mayan users. And there is no yesterday for everyone before certain First Day, but depending on user beleifs. So 2 extra "if" statements, every "Always" has exceptions
    – user7071
    Commented May 31, 2012 at 3:14
  • Lmao. Touche my friends.
    – Andy
    Commented May 31, 2012 at 4:45
  • 1
    The question depends on your definition of day: calendar day or 24 hour period? E.g. between monday and wednesday, there is one calendar day (tuesday) but 2 24-hour-periods (48 hours)
    – Flater
    Commented May 16, 2020 at 13:00

9 Answers 9

14

First what would make sense in your application if both dates were the same day? In my opinion the obvious answer is 0 and if that's your answer as well then the logical conclusion is to map dates that differ by one day to 1 and not 2. If you map dates that differ by a day to 2 then you have a gap and there is no way to map dates that don't coincide to 1. From a mathematical perspective things feel more consistent if there are no gaps.

6
  • I appreciate the input. That is very true, and you make a great point.
    – Andy
    Commented May 31, 2012 at 2:40
  • It greatly depends what a date means. That's the problem with dates. From a mathematical point a date has to be a point in time. But one day is 24 hours, that's where the problems come. If you say how much days are there between monday and tuesday, the answer can differ. If you ask how much days are there between monday 17:00 and tuesday 17:00 the answer is clear.
    – Pieter B
    Commented May 31, 2012 at 7:23
  • @PieterB - if you represent days as floats instead of ints, there is no problem with 24h days. The problem is how you round, but this is not specific to days.
    – mouviciel
    Commented May 31, 2012 at 7:34
  • @mouviciel the problem ins't whether you represent a day as an integer or float. The problem is that you should represent a date as a range and not as a point.
    – Pieter B
    Commented May 31, 2012 at 9:06
  • 1
    @PieterB - Actually, how many days there are between Monday 17:00 and Tuesday 17:00 is indefinite too, if either of the following are true - The dates are on opposite sides of a DST boundary, or are in different time zones. Days are not always 24 hours (and astronomically, they never are). The implicit assumption in a 'date' is that it's a specifically formatted/divided count of sunrises from some zero-point, at the observer's location. This question is more about counting 'sunrises' than the exact duration in a more accurate measure. Commented May 31, 2012 at 17:52
4

I would answer 1 because they are one day apart. There isn't a right answer to this though. It depends on the requirements for what you want to accomplish.

1
  • Hmm, good point. Thank you. And you do make a great point in saying they are 1 day apart.
    – Andy
    Commented May 31, 2012 at 2:38
2

I think it is a question of terminology and context.

If days between excludes the bounds, then the days between the two dates would be 0. Whereever this may be needed.

Date difference, i.e. how many days do I need to add to the start date to reach the end date: 1.

Or a date interval, including the bounds, e.g. how many leave days do I have from a start to an end date? Here the answer is 2.

2

I think the crux of the dilemma is that you've named the operation "calculating how many days between two dates". Clearly there are zero days between 3/1/12 and 3/2/12, but that more likely means that you've named the calculation wrong rather than done the wrong calculation.

Date arithmetic is notoriously tricky because of all the exceptions, so you really have to be rigorous in defining your requirements. For example, what does "now minus one day" translate into? You might say it means whatever time it was 24 hours ago, but I think that would be wrong in most applications. True, most days have 24 hours, but with daylight saving time one day has 23 hours and one day has 25 hours. I thank if it is noon on Sunday then now minus one day means noon on Saturday regardless of how many hours ago that was. I could go on with leap seconds and all the trouble they bring, as well as ensuring all the arithmetic identities and properties hold, but all of this is probably more work than you want to do.

Generally, when doing calendar date arithmetic people are interested in the "difference between dates". You can visualize this by imagining a paper calendar with the dates printed on it and putting a coin or stone or other marker on the starting date. If you are only allowed to move the marker to the immediately following date, how many moves would it take you to get to the ending date? That is the difference between the two dates in days.

One other practical consideration, though, is whether your application really includes the end date. It is typical for insurance contracts, for example, to not include the end date. Policies expire at 12:01 am on the end date. If you buy a policy that expires tomorrow, it's good for less than a day. On the other hand, in California, a "3 day notice to pay rent or quit" eviction notice gives you more than 3 days to get out. You have until the end of the third business day after the notice is served to pay the rent. So Wednesday + 3 days is actually Monday unless Monday is a holiday, in which case it is Tuesday and either way you get the whole final day, so a landlord cannot proceed until today > Wednesday + 3 days, i.e Tuesday or Wednesday. So it's not a straightforward calculation.

The good news is that because it comes up so often, there are libraries you can use to do the calculations and real life situations have reasonably well defined how they want date calculations done in order to clear up just these sorts of confusions.

So you really need to do your homework and correctly specify what interval you are talking about.

1

Both Dates, because that is what the users expect (in most cases)

I have worked on a few projects that had to work with logic surrounding dates, and expiration of entities. And there are some pitfalls.

The nature of those pitfalls is that what makes sense to a programmer isn't necessarily what makes sense to the people using the application.

People in the business would normally say stuff like, this thing is active from the 1. to the 31. of July. Now what they mean is that both dates are are included, i.e. it should be active for 31 days, not 30. But that defies normal arithmetic rules.

So if you take the question from the end users point of view, then in my experience the answer should be that you need to include both dates when calculating the number of days.

A few examples from my work experience:

I worked at creating an online job board, and we had plenty of problems with end dates for some entity types (expiration for a job, a subscription, etc). These would be specified as the last day this entity was still valid, which is what got stored in the database. But what made sense in the code would be to have the exact time when the entity would change state, i.e. at 00:00 the following date. That lead to very complex domain logic.

At some point, I refactored the entire system, adding one day to every EndDate in the database, and then moved the logic to the presentation layer, specifying that the actual date presented to the user should be the day preceding the one in the database. This lead to much, much cleaner domain logic regarding item expiration.

When I did the next project I worked with where date/entity expiration logic was important, I created a specific Date data type to encompass these rules, including overloading comparison operators with the build in DateTime class, arithmetic operators, etc. This lead to even simpler code, as it much better reflected the domain model, and the requirements for the presentation layer to subtract one day from all end days was removed, also removing the risk that someone would forget to implement this.

1

Ultimately, the correct answer to this is whatever your business requirements state.

However, when dealing with dates and time spans between dates, one generic way of looking at it to assume that any date that is expressed solely as a date (i.e. without a time portion) is to implicitly add 00:00 to the end of the date (midnight).

Now, calculate how many minutes of difference there is between the two date/times. So using your example (and I'm assuming US Date format here), how many minutes to you have to add, one at a time, to 2/1/2012 00:00 before you reach 3/1/2012 00:00 ?

Once you've done that, convert your minutes to hours, then to day (divide by 60, then divide result by 24) and you'll have your number of days between the two dates.

Using this, it turns out there's 1 day between 2/1/2012 00:00 and 3/1/2012 00:00 which seems to be both logical and in accordance with the general thinking of most people here and those who design well used functions in this area (i.e. Java's java.util.Date or .NET's System.DateTime etc.)

2
  • There's no time involved in this problem, only dates... Commented May 31, 2012 at 7:57
  • @SoboLAN - Yes, but in a way there is always an implicit time involved (like the implicit 00:00 when no time is actually specified). This makes it far easier to calculate spans of time between two dates. Once the result is calculated and expressed in complete days, the implicit time used as part of the calculation can be discarded.
    – CraigTP
    Commented May 31, 2012 at 9:15
0

You have to consider the context, if my working schedule is from monday to friday my boss will be a bit displeased if I only show up for 4 days.

That's because in this context, monday doesn't only stand for monday but for monday 8:30 and friday stands for friday 17:00.

0

The number of days ND between one date D1 and another date D2 is such that:

ND = D2 - D1

or:

D1 + ND = D2
0

It depends a lot on context, but in my opinion the obvious answer is to include 1 of the dates in the count.

Let's assume you count neither. And also let's assume there's an application that uses your algorithm to determine the difference of 2 dates typed by the user. In this case, the difference between 3/1/2012 and 3/2/2012 is 0. Is it then ok for the user to assume that the 2 dates are equal ? Because that's what 0 suggests: no difference between them -->> they're equal. It's probably wrong...

Let's then assume you count both. In this case, the difference will never be less than 2. Are there 2 days between 3/1/2012 and 3/2/2012 ? Certainly not. What about between 3/1/2012 and 3/1/2012 ? Again: no.

Given the fact that you only have dates as input and no time, I would do the algorithm like this:

  • if date 1 is equal to date 2, then the answer is 0
  • if date 1 is different than date 2, then the answer is the number of days between them + 1 (the "+ 1" means than one of the date is included in the count).

As I said in the beginning: this ultimately depends on your business requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.