# Calculate Day of the Year for a given date

I am trying to make sense of the following formula that claims to calculate the day of the year given today's date represented by (day/month/year):

N1 = floor(275 * month / 9)
N2 = floor((month + 9) / 12)
N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3))
N = N1 - (N2 * N3) + day - 30


Finally N supposedly refers to the day of the year.

What do the magic numbers 275,9 etc represent in the equation ?

• Short answer: 275/9 is roughly the number of days in an average month; there are significantly shorter formulas if you allow the use of IF/THEN statements and the modulo operator. – barrycarter Jun 11 '15 at 14:17
• Does this formula claim to be accurate before 1901 and after 2099? From 1901 to 2099, there is a leap year every 4 years, but neither 1900 nor 2100 are leap years. – barrycarter Jun 11 '15 at 14:23
• @barrycarter - I am sorry this is an old question and I dont even remember the source of this formula or the context in which I needed it. But you can easily write a test using something like Java time API's LocalDateTime class to verfiy if it gives the same results. – bhaskarc Jun 11 '15 at 16:19

This would really be better off in Stackoverflow, or another page. Presumably, you assumed some of the calculations rely on concepts in Astronomy, which as far as I can tell having gone through them, they do not with the exception of the concept of leap years; which I think is general enough that it doesn't really mandate being in Astronomy. Regardless:

The equation is a little abstract, so probably the easiest way to understand it would be through understanding each bit of data. Let's start with N2, I'll come back to N1:

N2 = floor((month + 9) / 12)


N2 will equal 0 if month is less than 3, and will equal 1 if it is greater. This formula is to determine whether February has passed.

N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3))


N3 will be equal to 2 if the year is not a leap year, and will be equal to 1 if it is a leap year. The math here is just to determine whether the current year happens to be a leap year.

N = N1 - (N2 * N3) + day - 30


Now we take N1 (which I haven't covered yet) and we subtract it by the product of N2 and N3, which will equal 0 if we haven't reached March, or if we have it will be 1 on a leap year, and 2 on a non-leap year. We then add the number of the current day of the month, and subtract 30. So what does N1 do?

N1 = floor(275 * month / 9)


Well, lets look at the values returned compared to days in a non-leap year:

Month     N1         Day       Diff
1.        30         31          -1
2.        61         59          +2
3.        91         90          +1
4.        122        120         +2
5.        152        151         +1
6.        183        181         +2
7.        213        212         +1
8.        244        243         +1
9.        275        273         +2
10.       305        304         +1
11.       336        334         +2
12.       366        365         +1


A bit messy, right? Now, remember that you're subtracting 30 from the total at the end to get N, and we're adding in the current date. This means that although we multiply our current month to get N1, we're actually using this to calculate the dates from the months prior to our current month! Thus if we take the value of N1, subtract it by 30, and compare it to the preceding month, the chart will come out like this:

Month     N1         Day       Diff
1.        31         31           0
2.        61         59          +2
3.        92         90          +2
4.        122        120         +2
5.        153        151         +2
6.        183        181         +2
7.        214        212         +2
8.        245        243         +2
9.        275        273         +2
10.       306        304         +2
11.       336        334         +2
12.       ---        365         ---


From this, you can see that the value of N1 will equal 2 greater than the actual date for any day in which it is March or later. This is perfect, as N2 is already a formula determining this for catching leap days. Note, these would all equal +1 on a leap year, as in another day would have been added in February. Thus coming back to the final calculation:

N = N1 - (N2 * N3) + day - 30


We take N1 and subtract in by the value of (N2 * N3), which will be 2 on a non-leap year if February has passed, 1 on a leap year, or 0 if we haven't passed February. We add in the days in the current month, and we subtract 30 to get the offset in N1 values.

This should consistently give you the day of the year. As for special significance, they're being used only for mathematical properties. Dividing a number between 1 and 12 by 9, just so happens to equal less than 1 for numbers less than 3, and greater than or equal to 1 and less than 2 all the way up to 12, which is why it is we have started March. The 275 * (month / 9) formula I'm not sure where they came up with. AFAIK, they have no special meaning to time or the Sun-Earth system - rather, they were chosen because they calculate well in this particular usage.

• Is this the same as Zeller's congruence? – Yashbhatt May 11 '14 at 17:52
• I'm not terribly familiar with Zeller's Congruence, but based on a quick look there are some similarities in general logical calculation, but the rest is dissimilar. Of course, this formula is simply used to calculate the numerical day of the given an arbitrary date, whereas Zeller's is for the day of the week given an arbitrary date. – Mitch Goshorn May 12 '14 at 1:47
• But the number 275 seems arbitrary. I feel it was just introduced to get the correct answer. – Yashbhatt May 12 '14 at 4:53
• I agree. The formula only has to accurately give useful result up to 12 months, so there is a lot of room for arbitrary values. I'd wager the code was built quite purposefully by an engineer. There are probably several potential solutions; this is just one of them. – Mitch Goshorn May 12 '14 at 11:31