2

Say you have a date, 30/03/2017, and you want to find out how many days are within that date from 00/00/0000. I know the days portion and year portion of the date can be expressed as:

cumulativeDays = year * 365 + year / 4 + days;

However, I don't know how to express the cumulative days given by the month portion of the date. I have found that the days in any given month can be given by:

f(x) = 28 + (x + floor(x / 8)) % 2 + 2 % x + 2 * floor(1 / x)

as detailed at the following link, but I can't transform the above formula into a summation of the days in each month. I intend to use a summation formula based upon the above to output the days up to the start of March, i.e. 31 + 28, which would then cover the month portion of the date. Could someone help me with creating this summation formula? I'm struggling with the summation of the mods specifically.

If anyone happens to have a better way of doing this I'm open to suggestions but I'd like the solution to be restricted to a single formula which given the year, month and day will outputs the cumulative days.

To clarify I'm attempting to make a formula to convert a given date, say today 30/03/2017, into days only. I.e. the date 02/01/0003 would be:

years = 3 * 365 + 3 / 4 = 1095
months = 31 (January only)
days = 2

So the cumulative days would then be:

1095 + 31 + 2 = 1128

Please note that I'm using this in a computing context and hence performing integer devision, so

3 / 4 = 0

not

3 / 4 = 0.75
Jyrki Lahtonen
  • 133,153
goastler
  • 121
  • Does $00/00/0000$ mean $01/01/0001$ i.e. starting of the first century? – Jaideep Khare Mar 30 '17 at 15:55
  • 00/00/0000 is just the minimum value, I'm using this idea in a computing context hence the day, month and year are just an integer – goastler Mar 30 '17 at 16:42
  • This is an ugly question, with all kinds of branches to deal with leap years and the lengths of months. This search finds several online sites and an Excel solution.You could reverse engineer that if you have to https://www.google.com/search?q=calculate+days+between+dates&ie=utf-8&oe=utf-8 – Ethan Bolker Mar 30 '17 at 16:56
  • Your formula doesn't seem to account for the non-leap years 1900, 2100 etc.: https://en.wikipedia.org/wiki/Gregorian_calendar – martin.koeberl Mar 30 '17 at 17:04
  • Why not just use a table of month end days instead of going through all of these contortions? Also, shifting the beginning of the year to 1 March and then shifting back later puts the leap day at the end of the year, which makes many of these computations much simpler. – amd Mar 30 '17 at 19:52

1 Answers1

0

Assuming that formula is correct (I only skimmed the link), then $\displaystyle \sum_{x=1}^{12} f(x)$ gives you the number of days in a year that isn't a leap year.

So you'll need to figure out the total number of years you're counting days for. I'm not sure what date 00/00/0000 is supposed to represent but assuming we start on January 1st in the year 1 A.D., then that's 2016 full years, plus 3 full months in 2017:

$$\left(2016 \cdot \sum_{x=1}^{12} f(x)\right) + 31 + 28 + 31$$

Then you need to add $1$ for each leap year in that range, i.e., each year where February has 29 days. This is left as an exercise for the reader. :)

Say there are $n$ leap years in that range from 1 A.D. to 2017. Then your final formula is:

$$\left(2016 \cdot \sum_{x=1}^{12} f(x)\right) + 31 + 28 + 31 + n$$

Note that this also doesn't take into account the usage of different calendars throughout history.

  • That's not entirely what I was asking, I'm finding it hard to phrase the question so apologies. I can calculate the days in the years already as stated in the first part of the question but cannot calculate the days in the months. The above answer accounts for the years, true, but doesn't provide a formula for the months – goastler Mar 30 '17 at 16:47