
Over a year ago, I
presented to your attention my SunCalc project , which allows you to conveniently study the trajectories of the sun and the phases of sunlight (sunset, dawn, various types of twilight, etc.) during a given day and at a given location.
Now I decided to release the code that performs the direct calculation of these values as a separate open source library and publish it along with examples and documentation on GitHub:
github.com/mourner/suncalc')
The library is based on formulas from an
article about the position of the sun on Astronomy Answers , published under the BSD license, takes ~ 1.5 kb, meets the strict
JSLint standards and works not only in the browser, but also on various server platforms such as
Node.js.Library usage example
Get an object with solar phase times for today in London:
var times = SunCalc.getTimes( new Date(), 51.5, -0.1); // (, , )
Show received dawn time:
alert( " : " + times.sunrise.getHours() + ':' + times.sunrise.getMinutes());
Get the position of the sun (azimuth and altitude) during the dawn:
var sunrisePos = SunCalc.getSunPosition(times.sunrise, 51.5, -0.1); // (, , )
Display dawn azimuth in degrees:
alert( " : " + (sunrisePos.azimuth * SunCalc.rad2deg).toFixed(2));
Return times
- sunrise : dawn (the top edge of the sun appears on the horizon)
- sunriseEnd : the end of dawn (the lower edge of the sun touches the horizon)
- goldenHourEnd : the end of the "golden hour" (soft light, the most appropriate time for photography)
- solarNoon : sunny afternoon (the sun is at its highest point)
- goldenHour : the beginning of the “golden hour”
- sunsetStart : the beginning of the sunset (the lower edge of the sun touches the horizon)
- sunset : sunset (the sun fully sets below the horizon, evening civil twilight begins - a time when the sun is already below the horizon, but it’s still light enough outside to do without artificial lighting)
- dusk : the beginning of the evening nautical twilight (the time when it is already quite dark, but you can still navigate the horizon to the sea
- nauticalDusk : the beginning of evening astronomical twilight (visually dark, but not enough for astronomical observations)
- night : the beginning of the night (dark enough for most astronomical observations)
- nightEnd : the end of the night (and the beginning of the morning astronomical twilight)
- nauticalDawn : the beginning of the morning nautical twilight
- dawn : morning dawn (the beginning of morning civil twilight)
You can read more about these solar phases in the
twilight article on Wikipedia .
You can also add phases manually, for example:
SunCalc.addTime(20, "runAwayFromBeach", "goToBeach");
This code will add to the results the
SunCalc.getTimes
runAwayFromBeach function (the moment when the sun rises above 20 °) and
goToBeach (when the sun sets below 20 °).
Possible uses
At first glance, sites for planning travels, for photographers, and the like, come to mind. But you can apply fantasy and make, for example, a design change on the site depending on the real time of the day. Or build light schedules from a certain angle throughout the year on real estate websites. I remember that I was even once written with gratitude from some air conditioner installation company, whose employees are now actively using
SunCalc in their work ... And there was also a letter from an Islamist society, which he helped count the right time for prayer. In general, if you think about the mass of applications!
I hope that you will find useful use of this small library. I would welcome comments and feedback in the comments. Thank!
update : added goldenHour, goldenHourEnd times and the ability to add your own times (corresponding to a certain height of the sun) manually.
update 2 : a good alternative for more serious tasks (calculations for the moon, planets, etc.) from the
middle -
github.com/monoid/starjs