Git Product home page Git Product logo

sunrise-sunset's Introduction

Sunrise-Sunset-Js

Calculate sunrise and sunset times in Javascript. Based loosely and indirectly on Kevin Boone's SunTimes Java implementation of the US Naval Observatory's algorithm. Works on both browser (IE11+) and Node.js, ~1kb minified.

Build Status Coverage Status

Install

Via npm:

npm install --save sunrise-sunset-js

Via script tag:

<script src="sunrise-sunset.js">

Usage

import { getSunrise, getSunset } from 'sunrise-sunset-js';
// Or if you use CommonJS imports:
// const { getSunrise, getSunset } = require('sunrise-sunset-js')

/** 
 * Sunset tonight at the Triggertrap office for today
 */
const sunset = getSunset(51.4541, -2.5920);


/** 
 * Sunrise at Stonehenge on midsummer's day 2000
 */
const sunrise = getSunrise(51.1788, -1.8262, new Date("2000-06-21"));

/** 
 * Combined with geolocation. Sunset tonight at your location.
 */
navigator.geolocation.getCurrentPosition(function(position) {
  console.log(getSunset(position.coords.latitude, position.coords.longitude));
});

/** 
 * Get all solar events for given year (sorted by time of occurrence)
 */
function getSolarEventsForYear(latitude, longitude, year) {
  const result = [];
  const start = new Date(year, 0, 1).getTime();
  for (let i = 0; i < 366; i++) {
    const d = new Date(start + (i * 24 * 60 * 60 * 1000));
    if (d.getFullYear() > year) break; // For non-leap year
    result.push(['sunrise', getSunrise(latitude, longitude, d)]);
    result.push(['sunset', getSunset(latitude, longitude, d)]);
  }
  return result.filter(event => Boolean(event[1])).sort((a, b) => a - b);
}
getSolarEventsForYear(51.1788, -1.8262, 2019);

For those who still don't use js bundlers, there's global SunriseSunsetJS object, which exposes the same getSunset and getSunrise methods (must include dist/index.js in your html first)

var sunset = SunriseSunsetJS.getSunset(51.4541, -2.5920);
var sunrise = SunriseSunsetJS.getSunrise(51.4541, -2.5920, new Date("2000-06-21"));

By Matt Kane (@ascorbic). Copyright © 2012 Triggertrap Ltd. All Rights Reserved.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, or connect to: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html

sunrise-sunset's People

Contributors

brodanoel avatar dependabot[bot] avatar fabulator avatar justingolden21 avatar robphilipp avatar udivankin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sunrise-sunset's Issues

Sunset can occur before sunrise

While technically correct, I dont think this is intuitive.
const d = new Date( 'may-26-2019' )
console.log( Solar.getSunrise( 38.88333, -77.03333, d ) )
console.log( Solar.getSunset( 38.88333, -77.03333, d ) )

prints
2019-05-26T09:48:18.374Z
2019-05-26T00:22:08.624Z

Note the sunset occurs before the sunrise

After the sunrise at 9:48, the next sunset (which is the one I was expecting) actually falls on may 27th at 00:23.

Invalid date

import { getSunrise } from 'sunrise-sunset-js';

let sunrise = getSunrise(72.6992, -77.9595, new Date("2022-11-13"));
console.log(sunrise);

sunrise = getSunrise(72.6992, -77.9595, new Date("2022-11-14"));
console.log(sunrise);

returns

2022-11-13T16:20:45.168Z
Invalid Date

any idea to get this working? Maybe a timezone problem?

invalid date

i am getting invalid date with getSunrise(67.8557995, 20.225282, new Date("2018-12-19"));

Calculated sunset is 17 hours early.

Sunset today in Phoenix, Arizona is 6:27 PM.

This module is instead returning Mon Mar 04 2019 01:27:28 GMT-0700 (Mountain Standard Time)

I used the browser navigator code from the readme.

navigator.geolocation.getCurrentPosition(function(position) {
   console.log(getSunset(position.coords.latitude, position.coords.longitude));
});

Here is the positional information returned from navigator.geolocation.getCurrentPosition:

Position {coords: Coordinates, timestamp: 1551720924647}
coords: Coordinates
accuracy: 689
altitude: null
altitudeAccuracy: null
heading: null
latitude: 33.4400483
longitude: -112.0655335
speed: null
__proto__: Coordinates
timestamp: 1551720924647
__proto__: Position

Using the latest version of Google Chrome on Windows 10.

SunCalc provides better results

I know this problem has been posted before and I know you based the code on Kevin Boone's code, but his Solunar code returns for Los Angeles:

Dec 31 2020, Sunrise: 06:59, Sunset: 16:55

SunCalc returns:

sunrise: 2020-12-31T15:25:27.510Z
sunset: 2021-01-01T01:02:54.632Z (note that in Zulu, today's sunset is on 01-01 for my Pacific time zone)

sunrise-sunset returns:

sunrise: 2020-12-31T15:24:21.201Z
sunset: 2020-12-31T01:02:22.328Z (sunset is in fact yesterday's sunset, not the sunset for 12-31 which of course is 01-01 in Zulu)

So with all due respect, when I ask for sunrise and sunset from Now, I expect these to be the values for today, not a combination of today and yesterday. You have the geocoordinates, so it is possible to ascertain exactly when the sunrise and sunset are for today in that geolocation.

So I am using the 4-year-old SunCalc code because it provides what I expect, not some values based on algorithms which don't appear to have been translated correctly from Mr. Boone's code.

Calculated sunrise is too early - Time Zone Problem?

Possible duplicate of #2, but to be sure, I created a new issue. Also, this one might be a bit easier to reproduce.

Setup:
We're checking the sunrise for Zermatt, Switzerland. Coordinates and Sunrise according to Google:

(Source of both images: Google.com)

Bug:
image
The calculated time (04:47 CEST) is 2 hours off.

the same can be reproduced using the following command
getSunrise(46.0207, 7.7491, new Date('Sat Apr 13 2019 21:51:00 GMT+0200 (Central European Summer Time)'));

I suspect that the difference is caused by the time zone offset between CEST and UTC.

Innaccuracies at extreme latitudes

During calculate(), when cosLocalHourAngle > 1 or < -1, then the sun does not actually rise or set at the location on the given day. It would be nice if the library provided some optional way to detect this, perhaps by returning null or throwing an error in these conditions.

Otherwise, this is perfect. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.