Formula and Method for Sunset Time
Sunset happens when the sun's center drops to a zenith angle of 90.833° below the local vertical — slightly more than a literal 90° because atmospheric refraction lifts the sun's apparent position by about 34 arcminutes and the sun's disk has a radius of about 16 arcminutes, so the upper limb is still visible when the geometric center is already below the horizon. This calculator implements the classic sunrise/sunset algorithm published by the U.S. Naval Observatory's Nautical Almanac Office (Almanac for Computers, 1990), which finds sunset from three quantities: the sun's ecliptic longitude on a given day, its declination δ (angle north or south of the celestial equator), and the local hour angle H at which the sun reaches the 90.833° zenith for your latitude.
How the calculation works
The algorithm proceeds in steps. First it converts your date to a day-of-year number N and your longitude to an hour offset (lngHour = longitude ÷ 15) to estimate an approximate time of sunset. From that it computes the sun's mean anomaly M and true ecliptic longitude L, then the sun's declination via sinδ = 0.39782 × sin(L). The local hour angle at sunset comes from cos H = [cos(90.833°) − sinδ×sin(latitude)] / [cosδ×cos(latitude)], solved as H = arccos(cos H). Converting H to hours (H ÷ 15) and combining it with the sun's right ascension gives the mean local time of sunset in Universal Time, which is then shifted by your entered UTC offset to produce a clock time you can read directly. If cos H falls outside [−1, 1], there is no valid sunset that day — the sun is either continuously up (cos H < −1) or never rises (cos H > 1), which happens only at high latitudes near the solstices.
Time zones, longitude sign, and daylight saving
Longitude is entered as positive for east and negative for west of the prime meridian (so New York is roughly −74° and Tokyo is roughly +140°). The UTC offset should reflect your actual local clock, including daylight saving time if it is in effect on the date you enter — for example, New York is UTC−5 in winter (EST) but UTC−4 in summer (EDT). Getting the offset wrong shifts the answer by whole hours even though the underlying astronomy is unaffected.
Accuracy and what this calculator does not model
This algorithm is accurate to roughly a minute for most dates and latitudes under standard atmospheric conditions. It does not account for your local horizon — hills, buildings, or trees can hide the sun before the geometric sunset time, and being at elevation (a mountain, a tall building) can expose the sun a little longer than the flat-horizon calculation predicts. Unusual atmospheric refraction (temperature inversions, extreme cold, or high humidity) can also shift the true horizon crossing by a minute or two from the standard prediction.