Calculating sunrise/sunset times in Javascript
Solution 1
The relevant code can be found at http://www.esrl.noaa.gov/gmd/grad/solcalc/
You need longitude, latitude, and date. It would be helpful to indicate if you want the answer in local time, or universal time. The corrections needed to get true accuracy (and the definition you use for "sunrise" and "sunset") all play a huge role in the effectiveness of the calculation.
If you just want to know "approximately" when the sun is level with the horizon "on the assumption of a spherical earth, a circular orbit around the sun, and without atmospheric distortion" - then the whole shooting match reduces to something quite manageable. But if you want the real answer you have to work through about 600 lines of script of the above website.
For approximations, you can see this earlier answer
Solution 2
SunCalc seems to do what you want
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
https://github.com/mourner/suncalc
Solution 3
Sun-time is enough if you use JavaScript as programming language of server-side (Node.js).
npm install sun-time ;
Then :
var sun=require('sun-time');
sun('Tunis') // -> i.e : return {rise:"05:00",set:"18:36"}
for more details
Solution 4
This javascript program
https://github.com/Triggertrap/sun-js
calculates sunrise and sunset times based only on the latitude and longitude of your location, which can be obtained automatically from GPS as follows:
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
It includes a readme file to explain how to use it. It calculates the zenith, and is very accurate.
Related videos on Youtube
Xcqtion
Updated on September 08, 2020Comments
-
Xcqtion about 3 years
I have a time, and a given latitude in degrees. Is there any method of calculating the time of sunrise and sunset in Javascript with this information?
-
elclanrs about 10 yearsOf course, there's always a way... Can you post your code so far?
-
Floris about 10 yearsThe very short answer to your question is "yes". A good question would say "I have found the following equation by googling 'time of sunset'", and tried implementing it like this. I was expecting to get X, but I am getting Y. What am I doing wrong?
-
scunliffe about 10 yearsWouldn't the longitude be a bit helpful? ;-) and do you have some sort of table to lookup against or are you accessing a REST API?
-
baldrs about 10 yearsNo, there's no such way. But here you can find enough information to do it: en.wikipedia.org/wiki/Sunrise_equation
-
-
Xcqtion about 10 yearsReally appreciate the answer! Based on what you gave, I should be able to method-ize the sunrise/sunsets (if all goes well). Again, thank you for your time!
-
Floris about 10 yearsGood luck with it! Sometimes all you need is a nudge.
-
Ahmed Fasih about 8 yearsThis is the right answer—SunCalc is perfect for browser and node.
-
Lucas P. about 5 yearsPerfect little library for my Node project. Thanks!
-
Gauthier over 2 yearsI didnt ask the question but i would have, and good on clarifying details. Even better for giving both answers. Thank you! ++