在 Javascript 中计算日出/日落时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18949074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:47:48  来源:igfitidea点击:

Calculating sunrise/sunset times in Javascript

javascript

提问by Xcqtion

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?

我有一个时间,以及一个给定的纬度(以度为单位)。有没有什么方法可以用这些信息在 Javascript 中计算日出和日落的时间?

采纳答案by Floris

The relevant code can be found at http://www.esrl.noaa.gov/gmd/grad/solcalc/

相关代码可以在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.

如果你只是想知道“大约”什么时候太阳与地平线“假设地球是球形的,围绕太阳的圆形轨道,并且没有大气失真”——那么整个射击比赛就会减少到相当可控的程度。但是如果你想要真正的答案,你必须通过上面网站的大约 600 行脚本。

For approximations, you can see this earlier answer

对于近似值,您可以看到这个较早的答案

回答by Epeli

SunCalc seems to do what you want

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

SunCalc 是一个小型的 BSD 许可的 JavaScript 库,用于计算给定位置和时间的太阳位置、日光相位(日出、日落、黄昏等的时间)、月亮位置和月相

https://github.com/mourner/suncalc

https://github.com/mourner/suncalc

回答by Abdennour TOUMI

Sun-timeis enough if you use JavaScript as programming language of server-side (Node.js).

如果您使用 JavaScript 作为服务器端(Node.js)的编程语言,Sun-time就足够了。

npm install sun-time ; 

Then :

然后 :

var sun=require('sun-time');
 sun('Tunis') // -> i.e : return {rise:"05:00",set:"18:36"}

for more details

更多细节

回答by pollaris

This javascript program

这个javascript程序

https://github.com/Triggertrap/sun-js

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:

仅根据您所在位置的纬度和经度计算日出和日落时间,这可以从 GPS 自动获取,如下所示:

<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.

它包含一个自述文件来解释如何使用它。它计算天顶,非常准确。