SunCalc

SunCalc is a tiny BSD-licensed JavaScript library for calculating sun position,
sunlight phases (times for sunrise, sunset, dusk, etc.),
moon position and lunar phase for the given location and time,
created by Vladimir Agafonkin (@mourner)
as a part of the SunCalc.net project.
This version is reworked and enhanced by @hypnos3. The output of the function is changed in the most times to objects with enhanced properies.
Most calculations are based on the formulas given in the excellent Astronomy Answers articles
about position of the sun
and the planets.
You can read about different twilight phases calculated by SunCalc
in the Twilight article on Wikipedia.
table of contents
- SunCalc
- table of contents
- changed in this library
- Usage example
- Reference
- Moon illumination, position and zenith angle
- Changelog
- 2.0.5 — April 04, 2022
- 2.0.4 — April 04, 2022
- 2.0.2 — March 29, 2022
- 2.0.1 — March 13, 2022
- 2.0.0 — March 13, 2022
- 1.8.0 — Dec 22, 2016
- 1.7.0 — Nov 11, 2015
- 1.6.0 — Oct 27, 2014
- 1.5.1 — May 16, 2014
- 1.4.0 — Apr 10, 2014
- 1.3.0 — Feb 21, 2014
- 1.2.0 — Mar 07, 2013
- 1.1.0 — Mar 06, 2013
- 1.0.0 — Dec 07, 2011
- 0.0.0 — Aug 25, 2011
changed in this library
| SunCalc.getTimes | SunCalc.getSunTimes |
| sunrise | sunriseEnd |
| sunset | sunsetStart |
| dawn | civilDawn |
| dusk | civilDusk |
| night | astronomicalDusk |
| nightEnd | astronomicalDawn |
| goldenHour | goldenHourDuskStart |
| goldenHourEnd | goldenHourDawnEnd |
Additional are the output of the function is changed in the most times to objects with more properies. Also JSDOC is added ans type script definitions.
Usage example
let times = SunCalc.getSunTimes(new Date(), 51.5, -0.1);
let sunriseStr = times.sunriseStart.getHours() + ':' + times.sunrise.getMinutes();
let sunrisePos = SunCalc.getPosition(times.sunrise, 51.5, -0.1);
let sunriseAzimuth = sunrisePos.azimuth * 180 / Math.PI;
SunCalc is also available as an NPM package:
$ npm install suncalc3
let SunCalc = require('suncalc3');
Reference
Sunlight times
SunCalc.getSunTimes(dateValue, lat, lng, height, addDeprecated, inUTC)
Returns an object with the following properties:
These properties contains the sun times for these given times:
astronomicalDawn | night ends (morning astronomical twilight starts) | 18 |
amateurDawn | amateur astronomical dawn (sun at 12° before sunrise) | 15 |
nauticalDawn | nautical dawn (morning nautical twilight starts) | 12 |
blueHourDawnStart | blue Hour start (time for special photography photos starts) | 8 |
civilDawn | dawn (morning nautical twilight ends, morning civil twilight starts) | 6 |
blueHourDawnEnd | blue Hour end (time for special photography photos end) | 4 |
goldenHourDawnStart | morning golden hour (soft light, best time for photography) starts | -1 |
sunriseStart | sunrise (top edge of the sun appears on the horizon) | 0.833 |
sunriseEnd | sunrise ends (bottom edge of the sun touches the horizon) | 0.3 |
goldenHourDawnEnd | morning golden hour (soft light, best time for photography) ends | -6 |
solarNoon | solar noon (sun is in the highest position) | |
goldenHourDuskStart | evening golden hour (soft light, best time for photography) starts | -6 |
sunsetStart | sunset starts (bottom edge of the sun touches the horizon) | 0.3 |
sunsetEnd | sunset (sun disappears below the horizon, evening civil twilight starts) | 0.833 |
goldenHourDuskEnd | evening golden hour (soft light, best time for photography) ends | 1 |
blueHourDuskStart | blue Hour start (time for special photography photos starts) | 4 |
civilDusk | dusk (evening nautical twilight starts) | 6 |
blueHourDuskEnd | blue Hour end (time for special photography photos end) | 8 |
nauticalDusk | nautical dusk end (evening astronomical twilight starts) | 12 |
amateurDusk | amateur astronomical dusk (sun at 12° after sunrise) | 15 |
astronomicalDusk | night starts (dark enough for astronomical observations) | 18 |
nadir | nadir (darkest moment of the night, sun is in the lowest position) | |
SunBH is the angle of the sun below the horizon
If addDeprecated is true, the object will have additional objects, with the same properties as other properties. This is to have backwards compatibility to original suncalc library.
dawn | civilDawn |
dusk | civilDusk |
nightEnd | astronomicalDawn |
night | astronomicalDusk |
nightStart | astronomicalDusk |
sunrise | sunriseStart |
sunset | sunsetEnd |
goldenHour | goldenHourDuskStart |
goldenHourEnd | goldenHourDawnEnd |
goldenHourStart | goldenHourDuskStart |
Each of the properties will be an object with the following properties:
adding / getting own Sunlight times
SunCalc.addTime(angleInDegrees, riseName, setName, risePos, setPos)
Adds a custom time when the sun reaches the given angle to results returned by SunCalc.getSunTimes.
- the function tests for validity of the given parameters
riseName and setName must be a non empty string and match the regex /^(?![0-9])[a-zA-Z0-9$_]+$/
angleInDegrees must be a number
originalName must be in the array SunCalc.times as riseName or setName
riseName and setName must not correspond to a riseName or setName already in the array SunCalc.times
Additional this function removes all items from SunCalc.timesDeprecated array where the riseName or setName matches the alternameName to prevent errors.
SunCalc.times property contains all currently defined times of type Array.<ISunTimeNames>.
SunCalc.addDeprecatedTimeName(alternameName, originalName)
Add a deprecated name
- the function tests for validity of the given parameters
alternameName must be a non empty string and match the regex /^(?![0-9])[a-zA-Z0-9$_]+$/
originalName must be in the array SunCalc.times as riseName or setName
alternameName must not correspond to a riseName or setName in the array SunCalc.times
SunCalc.timesDeprecated property contains all deprecated time names as an Array.<[string, string]> - Array.<deprecatedname, originalName>.
get specific Sunlight time
SunCalc.getSunTime(dateValue, lat, lng, elevationAngle, height, degree, inUTC)
Returns an object with the following properties:
rise and set will be an object equal to the times objects given by SunCalc.getSunTimes.
get Sunlight time for a given azimuth angle for a given date
SunCalc.getSunTimeByAzimuth(date, lat, lng, nazimuth, degree)
Returns an Date object
getting solar time
SunCalc.getSolarTime(dateValue, utcOffset, lng)
Returns an Date object
Sun position
SunCalc.getPosition(dateValue, lat, lng)
Returns an object with the following properties:
altitude: sun altitude above the horizon in radians,
e.g. 0 at the horizon and PI/2 at the zenith (straight over your head)
azimuth: sun azimuth in radians (direction along the horizon, measured from south to west),
e.g. 0 is south and Math.PI * 3/4 is northwest
Moon position
SunCalc.getMoonPosition(dateValue, lat, lng)
Returns an object with the following properties:
Moon illumination
SunCalc.getMoonIllumination(dateValue)
Returns an object with the following properties:
Moon phase value should be interpreted like this:
By subtracting the parallacticAngle from the angle one can get the zenith angle of the moons bright limb (anticlockwise).
The zenith angle can be used do draw the moon shape from the observers perspective (e.g. moon lying on its back). The SunCalc.getMoonData function will return the zenith angle.
SunCalc.moonCycles contains an array with objects of type IPhaseObj for every phase.
Moon illumination, position and zenith angle
SunCalc.getMoonData(dateValue, lat, lng)
Returns an object with the following properties:
The IMoonIllumination object is the same as the SunCalc.getMoonIllumination functions returns.
Moon rise and set times
SunCalc.getMoonTimes(dateValue, lat, lng, inUTC)
Returns an object with the following properties:
By default, it will search for moon rise and set during local user's day (frou 0 to 24 hours).
If inUTC is set to true, it will instead search the specified date from 0 to 24 UTC hours.
Moon transit
SunCalc.moonTransit(rise, set, lat, lng)
Returns an object with the following properties:
Changelog
2.0.5 — April 04, 2022
- function
SunCalc.addTime(...) removes all items from SunCalc.timesDeprecated array where the new rise or set name matches the alternameName.
2.0.4 — April 04, 2022
- added
SunCalc.addDeprecatedTimeName(...) function
- renamed
SunCalc.timesAlternate array to SunCalc.timesDeprecated
- added validation to function
addTime
2.0.2 — March 29, 2022
2.0.1 — March 13, 2022
2.0.0 — March 13, 2022
- published as suncalc3 after this library was used by my own with various changes to the original one
- added getSolarTime and moonTransit
1.8.0 — Dec 22, 2016
- Improved precision of moonrise/moonset calculations.
- Added
parallacticAngle calculation to getMoonPosition.
- Default to today's date in
getMoonIllumination.
- Fixed incompatibility when using Browserify/Webpack together with a global AMD loader.
1.7.0 — Nov 11, 2015
- Added
inUTC argument to getMoonTimes.
1.6.0 — Oct 27, 2014
- Added
SunCalc.getMoonTimes for calculating moon rise and set times.
1.5.1 — May 16, 2014
- Exposed
SunCalc.times property with defined daylight times.
- Slightly improved
SunCalc.getTimes performance.
1.4.0 — Apr 10, 2014
- Added
phase to SunCalc.getMoonIllumination results (moon phase).
- Switched from mocha to tape for tests.
1.3.0 — Feb 21, 2014
- Added
SunCalc.getMoonIllumination (in place of getMoonFraction) that returns an object with fraction and angle
(angle of illuminated limb of the moon).
1.2.0 — Mar 07, 2013
- Added
SunCalc.getMoonFraction function that returns illuminated fraction of the moon.
1.1.0 — Mar 06, 2013
- Added
SunCalc.getMoonPosition function.
- Added nadir (darkest time of the day, middle of the night).
- Added tests.
1.0.0 — Dec 07, 2011
- Published to NPM.
- Added
SunCalc.addTime function.
0.0.0 — Aug 25, 2011