# Finding the number of days between two dates given specific methods

Let's say I have two input dates of the format dd/mm/yyyy. The first is the current date and the second is the future date. I have methods getDay, getMonth, and getYear, which return to me integer values for day, month and year. I have another method called isLeapYear, that checks whether or not a particular year is a leap year or not (The criteria that it uses is that the year must be a multiple of 4 and for century years, the year must also be a multiple of 400 for it to be a leap year), and returns a true or false.

I know that this is somewhat of a computer science question, but the math underneath it is relevant here. How do I devise a formula that calculates the number of days between two dates? I had the idea that I could perhaps perhaps find the number of days from a fixed date A to the current date, then the number of days from A to the future date, and then subtract the difference, but i'm not sure exactly how I would do that?

I thought of using 00/00/0000 as the fixed date but i'm not sure if that makes sense. Would I consider the year 0000 to be a leap year or not? Any hints would be appreciated

You can create a fixed date to compare with that is easy to compute and $$00/00/0000$$ should be a good candidate though such date doesn't exists. We can use the idea that $$(p-a)-(q-a)=p-q$$

I will just focus on the task of finding $$p-a$$.

Assuming that there is no historical event such as change of calendar system in your question.

We can first compute the number of days that have went through ignoring the leap days,

$$365y + d + \sum^{m-1}_{i=1}m_i$$

where $$m_i$$ is the number of days in the $$i$$-th month, assuming $$m_2=28$$.

After that, I think what is more helpful is not to check whether a particular year is a leap year but to check how many leap years do we have so far. We have to add those leap days to our previous calculation.

To count how many leap years, just do $$\left\lfloor \frac{y}4\right\rfloor-\left\lfloor \frac{y}{100}\right\rfloor+\left\lfloor \frac{y}{400}\right\rfloor.$$

Also, we have to be careful about whether our current year is a leap year, if it is, we would want to check if the month is a month that is greater than $$2$$ and adjust accordingly.

One common way of doing this in software engineering is to store each date internally as an offset in the number of days from some specific date, regarded as the beginning of an era. It is most often January 1, 1970, but some others are used as well.

Now difference in dates is just an ordinary integer subtraction, and the real problem occurs when you need to compute this offset for a given date. That's relatively easy to do. You compute the number of days to the given (month,day) combo from December 31 of the previous year, mapping each month number into a corresponding offset of days, i.e. your month array on a regular year looks like $$31,31+28=59,59+31=90,90+30=120,\ldots$$ and this can be custom-adjusted for leap year's 1-day error, or a separate array can be stored and used when applicable.

Then the last thing to figure out is the year arithmetic...

• I'm not sure I understand what's going on here – user140161 Oct 29 '18 at 21:41
• @user140161 please ask what you do not understand exactly – gt6989b Oct 29 '18 at 21:56