suncalc icon indicating copy to clipboard operation
suncalc copied to clipboard

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

Open yorickvP opened this issue 12 years ago • 21 comments

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.

yorickvP avatar Nov 19 '13 23:11 yorickvP

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?

fchristant avatar Feb 11 '14 21:02 fchristant

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.

yorickvP avatar Feb 11 '14 21:02 yorickvP

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.

fchristant avatar Feb 11 '14 21:02 fchristant

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

yorickvP avatar Feb 11 '14 21:02 yorickvP

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.

fchristant avatar Feb 11 '14 21:02 fchristant

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

mourner avatar Feb 11 '14 23:02 mourner

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.

fchristant avatar Feb 12 '14 10:02 fchristant

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.

mourner avatar Feb 21 '14 19:02 mourner

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.

fchristant avatar Feb 21 '14 22:02 fchristant

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)

emcjh avatar Apr 23 '14 20:04 emcjh

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

mourner avatar Apr 23 '14 20:04 mourner

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.

emcjh avatar Apr 24 '14 12:04 emcjh

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

mourner avatar Apr 24 '14 16:04 mourner

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.

emcjh avatar Apr 24 '14 17:04 emcjh

@emcjh What happens if you do this:

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

fchristant avatar Apr 24 '14 18:04 fchristant

@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

emcjh avatar Apr 24 '14 18:04 emcjh

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.

fchristant avatar Apr 24 '14 19:04 fchristant

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

jonsadka avatar Nov 27 '14 08:11 jonsadka

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.

janett-baresel avatar Jul 25 '16 08:07 janett-baresel

+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);

krishnaglick avatar Oct 24 '16 18:10 krishnaglick

+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 !

Pierre-Gilles avatar Mar 17 '17 13:03 Pierre-Gilles