Why is_day Alone Won’t Save You: Building a Reliable Solar Noon and Daylight Window Calculator from WeatherAPI Fields

is_day is a 0 or 1. That’s the whole field. It flips to 1 at sunrise and back to 0 at sunset — which sounds fine until you realize you have no idea where in the daylight window you actually are, how fast the sun is rising or setting, or whether the current hour is closer to dawn, solar noon, or dusk. For any app that needs to know something about solar angle or light intensity, that binary flag is nearly useless.

WeatherAPI’s astronomy endpoint gives you enough to build something much more useful. It’s just not assembled for you.

What the astronomy endpoint actually returns

A request to /v1/astronomy.json?key=...&q=...&dt=YYYY-MM-DD returns: sunrise, sunset, moonrise, moonset, moon_phase, and moon_illumination. The times come back as strings in 12-hour format (e.g. "06:48 AM"), local to the queried location.

That’s it for the sun side. No solar elevation angle, no azimuth, no civil/nautical/astronomical twilight bounds. So the question is: what can you derive from just sunrise and sunset, and what genuinely requires going somewhere else?

Solar noon is free — and more useful than you’d think

Solar noon is the midpoint between sunrise and sunset. Not calendar noon. Not 12:00 PM local time. The actual midpoint of the daylight window, which shifts continuously across the year and varies by longitude within a time zone.

In Glasgow in mid-June, solar noon falls around 1:20 PM BST. In January it’s closer to 12:40 PM. If you’re rendering a solar arc visualization or building a “peak UV window” estimate, treating 12:00 as solar noon introduces a real error — small in absolute terms, meaningful when you’re trying to flag a two-hour window around peak sun.

// Assumes sunrise/sunset come back as "HH:MM AM/PM" strings
function parseSolarTime(timeStr, dateStr, tzOffsetHours) {
  const [time, meridiem] = timeStr.split(' ');
  let [hours, minutes] = time.split(':').map(Number);
  if (meridiem === 'PM' && hours !== 12) hours += 12;
  if (meridiem === 'AM' && hours === 12) hours = 0;

  const dt = new Date(`${dateStr}T${String(hours).padStart(2,'0')}:${String(minutes).padStart(2,'0')}:00`);
  dt.setMinutes(dt.getMinutes() - tzOffsetHours * 60);
  return dt; // UTC epoch
}

function solarNoon(sunriseStr, sunsetStr, dateStr, tzOffsetHours) {
  const rise = parseSolarTime(sunriseStr, dateStr, tzOffsetHours);
  const set = parseSolarTime(sunsetStr, dateStr, tzOffsetHours);
  return new Date((rise.getTime() + set.getTime()) / 2);
}

Use location.tz_id from the realtime endpoint and a proper IANA timezone library rather than a raw offset. Raw offsets break during DST transitions; named timezones don’t.

Daylight fraction: a more honest metric than is_day

Once you have sunrise, sunset, and the current local time, you can compute how far through the daylight window you are as a 0.0–1.0 fraction. That’s immediately useful for:

  • Solar panel output estimators — energy production follows a rough sine curve through the day, peaking near solar noon, not at clock noon
  • Photography apps that care about golden hour (roughly the first and last 10–15% of the daylight fraction)
  • Any UI that wants to render a smooth sun position rather than a binary day/night toggle
  • Agricultural apps estimating daily evapotranspiration, where the diurnal radiation curve matters
function daylightFraction(now, sunriseEpoch, sunsetEpoch) {
  if (now <= sunriseEpoch || now >= sunsetEpoch) return null; // nighttime
  return (now - sunriseEpoch) / (sunsetEpoch - sunriseEpoch);
}

function isGoldenHour(fraction, threshold = 0.12) {
  return fraction !== null && (fraction <= threshold || fraction >= (1 - threshold));
}

The 12% threshold is a rough heuristic — it corresponds to the sun being within about 10 degrees of the horizon at mid-latitudes, which is in the right neighborhood but won’t hold at extreme latitudes or in mountainous terrain. Flag it as an estimate in your UI.

Where this approach hits its ceiling

Sunrise and sunset times from an astronomy endpoint are calculated for a theoretical flat horizon. If your user is in a valley or on the east face of a ridge, actual first light and last light differ from the mathematical times — sometimes by 20–30 minutes in pronounced terrain. We handle the elevation side of this for temperature data via lapse-rate corrections, but there’s no terrain-shading correction in the astronomy fields. Worth knowing before you build an alpine app that claims precise golden hour windows.

Civil twilight (sun 6° below horizon) and nautical twilight (12°) aren’t in the response either. If you need those — for aviation apps flagging VFR conditions, marine apps, or anything that cares about ambient light before sunrise — you’d have to calculate them from a solar position formula like the one NOAA publishes, which requires latitude, longitude, day-of-year, and UTC time. WeatherAPI gives you the first two from the location object, and the astronomy times give you enough to derive the equation of time correction if you want to go that far. For civil twilight specifically, though, it’s cleaner to call a dedicated solar position library than to reverse-engineer it from sunrise/sunset strings.

Stitching it into a real request flow

The astronomy endpoint is a separate call from realtime or forecast — you don’t get astronomy data by default on a forecast request. You can either call /astronomy explicitly or pull from the forecast endpoint’s astronomy object at the day level, available under forecast.forecastday[n].astro. If you’re already pulling multi-day forecast data, that second route costs one fewer API call.

For apps that only need today’s daylight window, the astronomy endpoint alone is clean and cheap — not a heavy response, well within free-tier quotas for most use patterns.

A caveat about string parsing across locales

The 12-hour time format in astronomy responses is consistent in English, but if you’re running WeatherAPI calls server-side and your server locale is set to something unexpected, test your time string parsing explicitly. A brittle new Date("06:48 AM") call without an explicit date context will eventually give you a wrong-day interpretation or an unintended timezone assumption. Always attach the explicit date string and work in UTC internally.

The underlying astronomy calculation is solid. The parsing layer is where most bugs live.

If is_day is all you’re using right now and you need anything beyond a day/night toggle, solar noon and daylight fraction are the right starting point — they cost nothing extra once you have sunrise and sunset, and they’re the difference between telling your user “it’s daytime” and telling them something actually useful about the light.

Scroll to Top