mourner / suncalc

A tiny JavaScript library for calculating sun/moon positions and phases.
BSD 2-Clause "Simplified" License
3.1k stars 417 forks source link

How to correctly specify the Date #91

Closed Kevin-Dunlap closed 7 years ago

Kevin-Dunlap commented 7 years ago

Hi,

Thank you for your work on this project, it is great.

I am running into an issue around specifying the date. If I use "new Date()" for the date then all data returned is accurate but if I try to specify a date, such as "new Date(2017,1,29)" I get data for 2/28/17.

var times = suncalc.getTimes(new Date(2017,1,29), 42.7, -83.3);

What format do we need to use to specify a custom date?

Any help would be appreciated.

Thanks!

mourner commented 7 years ago

Can you please cite what new Date(2017,1,29).toString() outputs for you?

Kevin-Dunlap commented 7 years ago

That's weird, I get Wed Mar 01 2017 00:00:00 GMT-0500 (EST).

mourner commented 7 years ago

So what's weird with JS dates is that months are 0-based. So new Date(2017, 1, 29) actually means February under 1, but since there are only 28 days in February this year, it "shifts" to March 1.

Kevin-Dunlap commented 7 years ago

Ah, I feel a bit silly. Thanks for clarifying that for me and for the quick responses.