├── README.md └── sun.js /README.md: -------------------------------------------------------------------------------- 1 | sun.js 2 | ====== 3 | 4 | Calculate sunrise and sunset times in Javascript. 5 | 6 | This library extends the Javascript Date object, adding methods to calculate sunrise and sunset times. It also extends the Math object to add several useful support functions. If you don't like core JS types being extended you should probably rewrite this! 7 | 8 | Based loosely and indirectly on Kevin Boone's SunTimes Java implementation of the US Naval Observatory's algorithm. 9 | 10 | Usage is very simple: 11 | 12 | ```javascript 13 | 14 | //Sunset tonight at the Triggertrap office 15 | var sunset = new Date().sunset(51.4541, -2.5920); 16 | 17 | //Sunrise at Stonehenge on midsummer's day 2000 18 | 19 | var sunrise = new Date("2000-06-21").sunrise(51.1788, -1.8262); 20 | 21 | //Combine it with geolocation. Sunset tonight at your location. 22 | 23 | navigator.geolocation.getCurrentPosition(function(position) { 24 | var sunset = new Date().sunset(position.coords.latitude, position.coords.longitude); 25 | }); 26 | 27 | ``` 28 | 29 | By Matt Kane (@ascorbic). Copyright © 2012 Triggertrap Ltd. All Rights Reserved. 30 | 31 | This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General 32 | Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) 33 | any later version. 34 | This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied 35 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 36 | details. 37 | You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to 38 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, 39 | or connect to: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 40 | 41 | -------------------------------------------------------------------------------- /sun.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Sunrise/sunset script. By Matt Kane. 3 | * 4 | * Based loosely and indirectly on Kevin Boone's SunTimes Java implementation 5 | * of the US Naval Observatory's algorithm. 6 | * 7 | * Copyright © 2012 Triggertrap Ltd. All rights reserved. 8 | * 9 | * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General 10 | * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) 11 | * any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied 14 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 15 | * details. 16 | * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, 18 | * or connect to: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 19 | */ 20 | 21 | 22 | Date.prototype.sunrise = function(latitude, longitude, zenith) { 23 | return this.sunriseSet(latitude, longitude, true, zenith); 24 | } 25 | 26 | Date.prototype.sunset = function(latitude, longitude, zenith) { 27 | return this.sunriseSet(latitude, longitude, false, zenith); 28 | } 29 | 30 | Date.prototype.sunriseSet = function(latitude, longitude, sunrise, zenith) { 31 | if(!zenith) { 32 | zenith = 90.8333; 33 | } 34 | 35 | 36 | var hoursFromMeridian = longitude / Date.DEGREES_PER_HOUR, 37 | dayOfYear = this.getDayOfYear(), 38 | approxTimeOfEventInDays, 39 | sunMeanAnomaly, 40 | sunTrueLongitude, 41 | ascension, 42 | rightAscension, 43 | lQuadrant, 44 | raQuadrant, 45 | sinDec, 46 | cosDec, 47 | localHourAngle, 48 | localHour, 49 | localMeanTime, 50 | time; 51 | 52 | if (sunrise) { 53 | approxTimeOfEventInDays = dayOfYear + ((6 - hoursFromMeridian) / 24); 54 | } else { 55 | approxTimeOfEventInDays = dayOfYear + ((18.0 - hoursFromMeridian) / 24); 56 | } 57 | 58 | sunMeanAnomaly = (0.9856 * approxTimeOfEventInDays) - 3.289; 59 | 60 | sunTrueLongitude = sunMeanAnomaly + (1.916 * Math.sinDeg(sunMeanAnomaly)) + (0.020 * Math.sinDeg(2 * sunMeanAnomaly)) + 282.634; 61 | sunTrueLongitude = Math.mod(sunTrueLongitude, 360); 62 | 63 | ascension = 0.91764 * Math.tanDeg(sunTrueLongitude); 64 | rightAscension = 360 / (2 * Math.PI) * Math.atan(ascension); 65 | rightAscension = Math.mod(rightAscension, 360); 66 | 67 | lQuadrant = Math.floor(sunTrueLongitude / 90) * 90; 68 | raQuadrant = Math.floor(rightAscension / 90) * 90; 69 | rightAscension = rightAscension + (lQuadrant - raQuadrant); 70 | rightAscension /= Date.DEGREES_PER_HOUR; 71 | 72 | sinDec = 0.39782 * Math.sinDeg(sunTrueLongitude); 73 | cosDec = Math.cosDeg(Math.asinDeg(sinDec)); 74 | cosLocalHourAngle = ((Math.cosDeg(zenith)) - (sinDec * (Math.sinDeg(latitude)))) / (cosDec * (Math.cosDeg(latitude))); 75 | 76 | localHourAngle = Math.acosDeg(cosLocalHourAngle) 77 | 78 | if (sunrise) { 79 | localHourAngle = 360 - localHourAngle; 80 | } 81 | 82 | localHour = localHourAngle / Date.DEGREES_PER_HOUR; 83 | 84 | localMeanTime = localHour + rightAscension - (0.06571 * approxTimeOfEventInDays) - 6.622; 85 | 86 | time = localMeanTime - (longitude / Date.DEGREES_PER_HOUR); 87 | time = Math.mod(time, 24); 88 | 89 | var midnight = new Date(0); 90 | midnight.setUTCFullYear(this.getUTCFullYear()); 91 | midnight.setUTCMonth(this.getUTCMonth()); 92 | midnight.setUTCDate(this.getUTCDate()); 93 | 94 | 95 | 96 | var milli = midnight.getTime() + (time * 60 *60 * 1000); 97 | 98 | 99 | return new Date(milli); 100 | } 101 | 102 | Date.DEGREES_PER_HOUR = 360 / 24; 103 | 104 | 105 | // Utility functions 106 | 107 | Date.prototype.getDayOfYear = function() { 108 | var onejan = new Date(this.getFullYear(),0,1); 109 | return Math.ceil((this - onejan) / 86400000); 110 | } 111 | 112 | Math.degToRad = function(num) { 113 | return num * Math.PI / 180; 114 | } 115 | 116 | Math.radToDeg = function(radians){ 117 | return radians * 180.0 / Math.PI; 118 | } 119 | 120 | Math.sinDeg = function(deg) { 121 | return Math.sin(deg * 2.0 * Math.PI / 360.0); 122 | } 123 | 124 | 125 | Math.acosDeg = function(x) { 126 | return Math.acos(x) * 360.0 / (2 * Math.PI); 127 | } 128 | 129 | Math.asinDeg = function(x) { 130 | return Math.asin(x) * 360.0 / (2 * Math.PI); 131 | } 132 | 133 | 134 | Math.tanDeg = function(deg) { 135 | return Math.tan(deg * 2.0 * Math.PI / 360.0); 136 | } 137 | 138 | Math.cosDeg = function(deg) { 139 | return Math.cos(deg * 2.0 * Math.PI / 360.0); 140 | } 141 | 142 | Math.mod = function(a, b) { 143 | var result = a % b; 144 | if(result < 0) { 145 | result += b; 146 | } 147 | return result; 148 | } 149 | 150 | --------------------------------------------------------------------------------