Skip to content

getTimes returns a sunrise/sunset on the next day for certain lat/long #174

Closed

Description

@jordanwegener

Using getTimes for some coordinates (close to a TZ border) and times close to but before midnight appears to produce an incorrect sunset time that is on the following day.

To reproduce, call getTimes with:

{
  time: new Date('2022-04-14T23:40:15.817+08:00')
  lat: -14.414989274961663,
  lon: 128.52523583273717,
  timezone: 'Australia/Perth'
}

I get back:

{
  times: {
    solarNoon: 2022-04-15T03:27:08.981Z,
    nadir: 2022-04-14T15:27:08.981Z,
    sunrise: 2022-04-14T21:33:37.354Z,
    sunset: 2022-04-15T09:20:40.607Z,
    sunriseEnd: 2022-04-14T21:35:51.374Z,
    sunsetStart: 2022-04-15T09:18:26.587Z,
    dawn: 2022-04-14T21:12:00.824Z,
    dusk: 2022-04-15T09:42:17.137Z,
    nauticalDawn: 2022-04-14T20:47:00.521Z,
    nauticalDusk: 2022-04-15T10:07:17.441Z,
    nightEnd: 2022-04-14T20:22:04.702Z,
    night: 2022-04-15T10:32:13.259Z,
    goldenHourEnd: 2022-04-14T22:02:20.087Z,
    goldenHour: 2022-04-15T08:51:57.875Z
  }
}

converted to local date strings for Australia/Perth TZ

sunrise: '15/04/2022, 07:33:37'
sunset: '15/04/2022, 19:20:40'

Activity

  1. changed the title [-]getTimes returns a sunset on the next day for certain lat/long[/-] [+]getTimes returns a sunrise/sunset on the next day for certain lat/long[/+] on Nov 23, 2023
  2. iankim-dev commented on Feb 21, 2025

    @iankim-dev

    same issue

  3. mourner commented on Jun 18, 2026

    @mourner
    Owner

    This is fixed in SunCalc v2.0.0. It's the same root cause as #11/#161/#185: the old getTimes bucketed the date by the nearest solar transit, so a time late in the local evening (your 23:40, which is 2022-04-14T15:40Z) got pushed onto the next day's events. v2 anchors to the UTC solar day containing the instant, so the result no longer depends on time-of-day.

    Verified against your exact inputs:

    getTimes(2022-04-14T23:40:15.817+08:00, -14.41499, 128.52524)
      sunrise UTC 2022-04-13T21:32:28Z → 14/04/2022 05:32 Perth
      sunset  UTC 2022-04-14T09:19:52Z → 14/04/2022 17:19 Perth  ✓
    

    Both events now fall on April 14 as expected. Closing as resolved.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions