-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (41 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module.exports = function moonCalc(date) {
if (!date instanceof Date) {
throw new TypeError("MoonCalc needs a date object");
}
const Moon = {
phases: ['new-moon', 'waxing-crescent', 'waxing-quarter', 'waxing-gibbous', 'full-moon', 'waning-gibbous', 'waning-quarter', 'waning-crescent'],
phase: function (year, month, day) {
let c = e = jd = b = 0;
if (month < 3) {
year--;
month += 12;
}
++month;
c = 365.25 * year;
e = 30.6 * month;
jd = c + e + day - 694039.09; // jd is total days elapsed
jd /= 29.5305882; // divide by the moon cycle
b = parseInt(jd); // int(jd) -> b, take integer part of jd
jd -= b; // subtract integer part to leave fractional part of original jd
b = Math.round(jd * 8); // scale fraction from 0-8 and round
if (b >= 8) b = 0; // 0 and 8 are the same so turn 8 into 0
return {phase: b, name: Moon.phases[b]};
}
};
let dd = date.getDate();
let mm = date.getMonth()+1; //January is 0!
let yyyy = date.getFullYear();
let phase = Moon.phase(yyyy, mm, dd);
let phaseStr = phase.name.replace(/[_-]/g, " ");
return phase
/*
var img = '/lib/img/weather/moon/' + phase.name + '.png';
var imgEl = document.createElement('img');
imgEl.src = '/lib/img/weather/moon/' + phase.name + '.png';
imgEl.alt = phaseStr;
var insert = document.getElementsByClassName("lunar");
insert[0].insertBefore(imgEl, insert[0].childNodes[0]);
var span = document.getElementsByClassName("phase");
span[0].innerHTML = phaseStr;
*/
}