Here is the formula;
d = (int)(30.6*((month + 9) % 12) + 58.5 + date) % 365;
All the variables are int.
Month is the month of the year 1...12
Date is the day of the month 1...31
The formula sets 'd' to be the day of the year 0...364
where 0 is the 1/Jan and 364 is the 31/Dec.
The formula does not include the year and so it cannot compensate for leap years.
For 29/Feb the formula returns 59 which is the same as 1/Mar so in leap years day 59 occurs twice.
I was unaware of this formula until recently.
My questions are;
- Is it well known
- Can anybody explain simply how it works
The formula does work. Below is some output which I hope is self explanatory.
I printed the returned values as if each month had 31 days.
31 28 31 30 31 30 31 31 30 31 30 31
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
01 0 31 59 90 120 151 181 212 243 273 304 334
02 1 32 60 91 121 152 182 213 244 274 305 335
03 2 33 61 92 122 153 183 214 245 275 306 336
04 3 34 62 93 123 154 184 215 246 276 307 337
05 4 35 63 94 124 155 185 216 247 277 308 338
06 5 36 64 95 125 156 186 217 248 278 309 339
07 6 37 65 96 126 157 187 218 249 279 310 340
08 7 38 66 97 127 158 188 219 250 280 311 341
09 8 39 67 98 128 159 189 220 251 281 312 342
10 9 40 68 99 129 160 190 221 252 282 313 343
11 10 41 69 100 130 161 191 222 253 283 314 344
12 11 42 70 101 131 162 192 223 254 284 315 345
13 12 43 71 102 132 163 193 224 255 285 316 346
14 13 44 72 103 133 164 194 225 256 286 317 347
15 14 45 73 104 134 165 195 226 257 287 318 348
16 15 46 74 105 135 166 196 227 258 288 319 349
17 16 47 75 106 136 167 197 228 259 289 320 350
18 17 48 76 107 137 168 198 229 260 290 321 351
19 18 49 77 108 138 169 199 230 261 291 322 352
20 19 50 78 109 139 170 200 231 262 292 323 353
21 20 51 79 110 140 171 201 232 263 293 324 354
22 21 52 80 111 141 172 202 233 264 294 325 355
23 22 53 81 112 142 173 203 234 265 295 326 356
24 23 54 82 113 143 174 204 235 266 296 327 357
25 24 55 83 114 144 175 205 236 267 297 328 358
26 25 56 84 115 145 176 206 237 268 298 329 359
27 26 57 85 116 146 177 207 238 269 299 330 360
28 27 58 86 117 147 178 208 239 270 300 331 361
29 28 59 87 118 148 179 209 240 271 301 332 362
30 29 60 88 119 149 180 210 241 272 302 333 363
31 30 61 89 120 150 181 211 242 273 303 334 364