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.