Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dates with times before a certain time return times for previous day #11

Open
yorickvP opened this issue Nov 19, 2013 · 21 comments
Open

Comments

@yorickvP
Copy link

see title, when inputting a date that has a time before 00:40:54, the times are for the previous day.
suncalc.getTimes(new Date(2013, 10, 20, 0, 40, 53), 52.2, 5.1)
returns times for november 19th, but a second later returns times for november 20th as expected.

@fchristant
Copy link

I have a similar issue, this date...

Wed Feb 12 2014 00:47:44 GMT+0100

...which is a console.log on a valid js Date object, gives me back the SunCalc times for the day before. This bug destroys the accuracy and credibility of what I'm trying to build. Did you ever get a fix for your issue?

@yorickvP
Copy link
Author

As far as I know, the issue is not fixed yet. However, a simple workaround is just to set the hours to the mid of the day, you'll get the correct times that way.

@fchristant
Copy link

Thanks, that's going to be some serious refactoring for me as in the real-time system I'm building the date is also a display date. Still hoping for a real fix, but I see little activity here.

@yorickvP
Copy link
Author

replace your gettimes calls with a suncalc.getTimes(new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12, 0, 0, 0, 0), lat, long)

@fchristant
Copy link

Thanks, that is helpful. It still means I need to split the display time from the internal time, but that looks like the only way forward, and doable.

@mourner
Copy link
Owner

mourner commented Feb 11, 2014

@fchristant while I'm going to fix the problem, please don't complain — this is open source, if something's not working for you, I'm happily accepting pull requests. There's little activity on this repo because it's pretty much feature complete, this is a low priority bug because there's an easy workaround, and I'm focused on more important things like https://github.com/Leaflet/Leaflet.

@fchristant
Copy link

I'm not complaining, I'm reporting an issue. With "little activity" I mean activity on the issues, as in communication. I'm in favor of at least a basic acknowledgement, even a message saying "sorry, I won't fix this" is better than nothing. Anyway, thanks for clarifying.

@mourner
Copy link
Owner

mourner commented Feb 21, 2014

OK, now I see what's going on here — SunCalc calculates all the times after it gets the time of the nearest solar transit. But solar transit isn't strictly 12am — e.g. if it's 12:44am, then 0:43am as input will result in dates of the previous day since its transit is closer.

@fchristant
Copy link

Thanks for the explanation. I did manage to work around the issue using the trick above, so no issues. I actually just launched a first version of a visualization that combines most info that your lib returns (except for azimuth):

http://www.jungledragon.com/daylight

This one is optimized for (wildlife) photographers. It has stateful URLs and one can even add a "light period" appointment to one's calendar. I'm sharing this just to let you know how valuable you library is. So thank you very much, and I hope you'll see better days in your country soon.

@emcjh
Copy link

emcjh commented Apr 23, 2014

Not sure if the issue I'm having is related but my dates all seem wrong.
I'm making the call like this:
var today = new Date();
var t = SunCalc.getTimes(today, 34.2, 84.1);
console.log(today);
console.log(t)

In the console this is what I see... first is valid js date followed by dump of my t var. It's showing dawn time as evening of day before and dusk as morning of current day...

Wed Apr 23 2014 15:46:54 GMT-0400 (Eastern Daylight Time)

Object {solarNoon: Wed Apr 23 2014 02:23:06 GMT-0400 (Eastern Daylight Time), nadir: Tue Apr 22 2014 14:23:06 GMT-0400 (Eastern Daylight Time), sunrise: Tue Apr 22 2014 19:44:26 GMT-0400 (Eastern Daylight Time), sunset: Wed Apr 23 2014 09:01:45 GMT-0400 (Eastern Daylight Time), sunriseEnd: Tue Apr 22 2014 19:47:06 GMT-0400 (Eastern Daylight Time)…}

dawn: Tue Apr 22 2014 19:18:13 GMT-0400 (Eastern Daylight Time)
dusk: Wed Apr 23 2014 09:27:59 GMT-0400 (Eastern Daylight Time)

@mourner
Copy link
Owner

mourner commented Apr 23, 2014

@emcjh it shows you times in your local time zone, you need to convert them according to the time zone of the location if you want precise local times and dates.

@emcjh
Copy link

emcjh commented Apr 24, 2014

Not sure I understand your comment. My local time zone and the time zone of the location I want to get dawn/dusk times are the same. If I submit a date of Apr 23, 2014 shouldn't I get the dawn time for Apr 23 and dusk time for April 23? Why does dawn show evening of Apr 22 and dusk as morning of Apr 23? Or am I just really missing something.

@mourner
Copy link
Owner

mourner commented Apr 24, 2014

If your timezones are good, why is sunrise happening at 7:44pm?

@emcjh
Copy link

emcjh commented Apr 24, 2014

That's exactly what I'm trying to figure out. I called getTimes() with a date of Apr 23 so why is sunrise returned as 7:44pm of Apr 22? Pretty sure I'm using the latest version of suncalc.js as I just downloaded it from here yesterday. Thanks for your assistance with this.

@fchristant
Copy link

@emcjh What happens if you do this:

var today = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12, 0, 0, 0, 0);

@emcjh
Copy link

emcjh commented Apr 24, 2014

@fchristant Dates and times still appear incorrect. I assumed d in your example is a js date object.

My code:
var d = new Date();
var today = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 12, 0, 0, 0, 0);
var t = SunCalc.getTimes(today, 34.2, 84.1);
console.log(today);
console.log(t);

Results in console:

Thu Apr 24 2014 12:00:00 GMT-0400 (Eastern Daylight Time) (index):1844
Object {solarNoon: Thu Apr 24 2014 02:22:55 GMT-0400 (Eastern Daylight Time), nadir: Wed Apr 23 2014 14:22:55 GMT-0400 (Eastern Daylight Time), sunrise: Wed Apr 23 2014 19:43:18 GMT-0400 (Eastern Daylight Time), sunset: Thu Apr 24 2014 09:02:33 GMT-0400 (Eastern Daylight Time), sunriseEnd: Wed Apr 23 2014 19:45:58 GMT-0400 (Eastern Daylight Time)…}
dawn: Wed Apr 23 2014 19:17:00 GMT-0400 (Eastern Daylight Time)
dusk: Thu Apr 24 2014 09:28:50 GMT-0400 (Eastern Daylight Time)
goldenHour: Thu Apr 24 2014 08:28:32 GMT-0400 (Eastern Daylight Time)
goldenHourEnd: Wed Apr 23 2014 20:17:19 GMT-0400 (Eastern Daylight Time)
nadir: Wed Apr 23 2014 14:22:55 GMT-0400 (Eastern Daylight Time)
nauticalDawn: Wed Apr 23 2014 18:45:38 GMT-0400 (Eastern Daylight Time)
nauticalDusk: Thu Apr 24 2014 10:00:12 GMT-0400 (Eastern Daylight Time)
night: Thu Apr 24 2014 10:32:53 GMT-0400 (Eastern Daylight Time)
nightEnd: Wed Apr 23 2014 18:12:57 GMT-0400 (Eastern Daylight Time)
solarNoon: Thu Apr 24 2014 02:22:55 GMT-0400 (Eastern Daylight Time)
sunrise: Wed Apr 23 2014 19:43:18 GMT-0400 (Eastern Daylight Time)
sunriseEnd: Wed Apr 23 2014 19:45:58 GMT-0400 (Eastern Daylight Time)
sunset: Thu Apr 24 2014 09:02:33 GMT-0400 (Eastern Daylight Time)
sunsetStart: Thu Apr 24 2014 08:59:53 GMT-0400 (Eastern Daylight Time)
proto: Object

@fchristant
Copy link

hmmm, well at least this means your issue is not related to the issue described at the beginning of the thread, we just excluded that.

@jonsadka
Copy link

No issue for me. It just required converting the result times to local timezone. Thanks!

@janett-baresel
Copy link

janett-baresel commented Jul 25, 2016

I still experience this issue. Here an example:

  • calculate sunrise and sunset for Sydney, Australia, while being in European timezone
// in Sydney this would be 23:59 March 15th
var currentDate = new Date("Sun Mar 15 2015 14:59:00 GMT+0100 (Central Europe Standard Time)");
var latitude = -33.87644639725535;
var longitude = 151.20731600000002;

var dates = SunCalc.getTimes(currentDate, latitude, longitude);

console.debug(dates);

This returns:
sunrise: Sun Mar 15 2015 20:55:56 GMT+0100 (Central Europe Standard Time)
sunset: Mon Mar 16 2015 09:14:51 GMT+0100 (Central Europe Standard Time)

If you calculate the local time for Sydney this will be:
sunrise: Mon Mar 16 2015 05:55:56 GMT+1000
sunset: Mon Mar 16 2015 18:14:51 GMT+1000

This means that for the date and time of 15th of March 23:59 it returns the sunrise and sunset for the following day.

I have tested this with the latest version of the library. It seems to me that the conversion into and from julian could be the reason. But I am not an expert and don't understand the calculations there, so it could be something else as well.
The change to 12 o'clock midday, as suggested above, should not be able to fix this, because depending on the local timezone of the computer and the long/lat of the target location this again ends up close to midnight for some locations.

@krishnaglick
Copy link

+1 on this issue, currently hacking around it w/ the midday line but would love a fix!
const midday = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0, 0, 0);

@Pierre-Gilles
Copy link

+1 I have the same problem in Gladys ( It's a home assistant project => https://github.com/GladysProject/Gladys ).

I calculate the sunrise at midnight, and it calculates for the day before
I will use the hack, but would love a fix :)

Thx a lot for this awesome library !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants