0

Good evening guys, I have little problem, I have to calculate how many days do I have between two dates which contains specific day, month and even year, do you guys have any idea how should I do that? Is there any formula, every formula was containing only month and year but not day...

By the way I dont know which tag should I give....

Robert Soupe
  • 14,663
Sanady
  • 11
  • For each full year between two dates, add $365$ days or $366$ days to the total depending on if it contains a leap year. For each full month between two dates, add $28,29,30$ or $31$ to the total depending on what month it is and whether it contains a leap year. Then finally include the number of days. A final concise formula will be tedious and include several if-then statements. – JMoravitz Mar 06 '18 at 21:17
  • It's okay you didn't know about [tag:calendar-computations]. – Robert Soupe Mar 07 '18 at 01:41
  • To get an understanding of what you're going for: would it be like something like how many days are there between December 7, 1941 and September 2, 1945? – Robert Soupe Mar 07 '18 at 01:43
  • You may be interested in the algorithm for converting a calendar date to a Julian day number. https://en.wikipedia.org/wiki/Julian_day#Converting_Gregorian_calendar_date_to_Julian_Day_Number – awkward Mar 07 '18 at 13:57

1 Answers1

1

For the Gregorian Calender:

1 common year = 365 days

One calendar leap year has 366 days:

Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.

Using this, you should be able to compute the number of days between two dates. Of course, you can use this to get a formula:

Given date1 = $(d1, m1, y1)$ and date2 = $(d2, m2, y2)$. Let date 2 be the later date. Then the number of days = $\sum(365n1+366n2$ $+(d2-d1)$+$d(\delta m))$. Where $n1$ and $n2$ are the number of normal years and leap years between the two dates and $d(\delta m)$ gives the difference in days caused by the difference in months, which could vary depending on the year and month.

Jesse Meng
  • 1,310
  • Aha ok, so this is for Gregorian calendar, is there any possible way to do this but for Julian one? – Sanady Mar 06 '18 at 21:39
  • @Sanady I believe for Julian it would be even easier, since you won't have the conditions of divisiblity by 100 or 400. You would simply have to take into account of the leap year which occurs every 4 years. The rest of the logic stays roughly the same. – Jesse Meng Mar 06 '18 at 21:49