A javascript library to calculate solar position and sun rise, set and transit times. It is based on the calculation and code published by National Renewable Energy Laboratory here, and should be very accurate.
The code is available on GitHub here.
The part of this code that calculates Julian days can be used separately. Julian Days are a date format commonly used in astronomy. They count the number of days since noon, January 1st, 4713 BC. Time periods less than a day can be represented as fractional days.
There are four ways to create a JulianDay object.
var jd = JulianDay.fromValues(2012, 1, 1, 13, 20, 05, 988); console.log(jd.ephemerisDay(60)); console.log(jd.ephemerisCentury(60)); console.log(jd.ephemerisMillennium(60)); var date = jd.toDate();
More ways of manipulating the time are available with the Time javascript class. Internally, Time instances store the time as fractions of a day.
They can be created
To actually perform the calculation, create a SolarPosition object providing as many of the parameters as you can.
var julianDay = JulianDay.fromValues(2012, 1, 1, 13, 20, 05, 988); var sp = new SolarPosition(julianDay, latitude, longitude, delta_t, elevation, pressure, temperature, slope, azm_rotation, atmos_refract); console.log(sp.getSunRiseTransitSet()); console.log(sp.getSunPosition()); console.log(sp.calculateGeocentricSunRightAscensionAndDeclination());