24

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?

Xcqtion
  • 251
  • 1
  • 2
  • 4
  • 3
    Of course, there's always a way... Can you post your code so far? – elclanrs Sep 22 '13 at 21:45
  • 3
    The 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? – Floris Sep 22 '13 at 21:46
  • 2
    Wouldn'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? – scunliffe Sep 22 '13 at 21:48
  • 2
    No, there's no such way. But here you can find enough information to do it: http://en.wikipedia.org/wiki/Sunrise_equation – baldrs Sep 22 '13 at 21:48
  • There is such a way (assuming you really mean to say "given latitude, longitude and date" - since all of those matter) and it is published by NOAA http://www.esrl.noaa.gov/gmd/grad/solcalc/sunrise.html . You can even see the javascript. One minute with Google... Another 30 seconds gave the most updated version: http://www.esrl.noaa.gov/gmd/grad/solcalc/ – Floris Sep 22 '13 at 21:51

4 Answers4

36

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

esamatti
  • 18,293
  • 11
  • 75
  • 82
10

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

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122
  • Really 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! – Xcqtion Sep 23 '13 at 00:49
  • Good luck with it! Sometimes all you need is a nudge. – Floris Sep 23 '13 at 01:16
  • I didnt ask the question but i would have, and good on clarifying details. Even better for giving both answers. Thank you! ++ – Gauthier Jul 20 '21 at 17:54
4

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

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
2

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.

pollaris
  • 1,281
  • 17
  • 20