<p>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:</p>

<p>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:</p>

<pre><code>N2 = floor((month + 9) / 12)
</code></pre>

<p>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.</p>

<pre><code>N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3))
</code></pre>

<p>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.</p>

<pre><code>N = N1 - (N2 * N3) + day - 30
</code></pre>

<p>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?</p>

<pre><code>N1 = floor(275 * month / 9)
</code></pre>

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

<pre><code>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
</code></pre>

<p>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 <em>prior</em> 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:</p>

<pre><code>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         ---
</code></pre>

<p>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:</p>

<pre><code>N = N1 - (N2 * N3) + day - 30
</code></pre>

<p>We take <code>N1</code> and subtract in by the value of <code>(N2 * N3)</code>, 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 <code>N1</code> values.</p>

<p>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 <code>275 * (month / 9)</code> 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.</p>

CC BY-SA 3.0
2014-05-11T17:52:18.453Z
Is this the same as Zeller's congruence?
Yashbhatt
CC BY-SA 3.0
2014-05-12T01:47:02.473Z
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
CC BY-SA 3.0
2014-05-12T04:53:41.983Z
But the number 275 seems arbitrary. I feel it was just introduced to get the correct answer.
Yashbhatt
CC BY-SA 3.0
2014-05-12T11:31:31.523Z
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
CC BY-SA 3.0
2014-05-11T12:34:39.763Z
2014-05-12T01:36:44.607Z
2014-05-12T01:36:44.607Z
Mitch Goshorn
<p>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):</p>

<pre><code>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
</code></pre>

<p>Finally N supposedly refers to the day of the year.</p>

<p>What do the magic numbers 275,9 etc represent in the equation ?</p>

CC BY-SA 3.0
2015-06-11T16:19:01.200Z
@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
<earth>
Calculate Day of the Year for a given date
bhaskarc
# Calculate Day of the Year for a given date

(Asked by: username_0 on 2014-05-11)

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 ?

**Tags:** earth

> **Comments:**
> **username_0**: @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.  

---

## 1 answers

### Answer by username_1 on 2014-05-11. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_2**: Is this the same as Zeller's congruence?  
> **username_1**: 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.  
> **username_2**: But the number 275 seems arbitrary. I feel it was just introduced to get the correct answer.  
> **username_1**: 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.  

---

