Find cumulative days in a date
Matthew Martinez
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 = 2So the cumulative days would then be:
1095 + 31 + 2 = 1128Please note that I'm using this in a computing context and hence performing integer devision, so
3 / 4 = 0not
3 / 4 = 0.75 $\endgroup$ 5 1 Answer
$\begingroup$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.
$\endgroup$ 1