Calculating sunrise/sunset times in Javascript

20,724

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.

Share:
20,724

Related videos on Youtube

Xcqtion
Author by

Xcqtion

Updated on September 08, 2020

Comments

  • Xcqtion
    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
      elclanrs about 10 years
      Of course, there's always a way... Can you post your code so far?
    • Floris
      Floris about 10 years
      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?
    • scunliffe
      scunliffe about 10 years
      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?
    • baldrs
      baldrs about 10 years
      No, there's no such way. But here you can find enough information to do it: en.wikipedia.org/wiki/Sunrise_equation
  • Xcqtion
    Xcqtion about 10 years
    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!
  • Floris
    Floris about 10 years
    Good luck with it! Sometimes all you need is a nudge.
  • Ahmed Fasih
    Ahmed Fasih about 8 years
    This is the right answer—SunCalc is perfect for browser and node.
  • Lucas P.
    Lucas P. about 5 years
    Perfect little library for my Node project. Thanks!
  • Gauthier
    Gauthier over 2 years
    I didnt ask the question but i would have, and good on clarifying details. Even better for giving both answers. Thank you! ++

Related