Physics Stack Exchange is a question and answer site for active researchers, academics and students of physics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a simple method of determining, given a UTC date/time, whether it is day or night at a given lat/long coordinate?

I am currently using a formula based on a Sunrise/Sunset Algorithm from the US Naval Observatory, but unfortunately I am having trouble applying to arrive at a simple night/day indicator.

Should I build upon this formula or is there some more efficient way of doing this that I am missing?

Disclaimer: I am a programmer, not an astronomer, so I find the formulas for astronomical calculations a bit confusing.

Extra stuff: Ideally I would like to artificially shorten the period of "night" by two hours, for the purposes of eliminating some false positives from sensor data that occurs during twilight/dawn. In other words, if sunset occurs at 7 pm at a given location, I would like the option of padding the value by 60 minutes such that the output remains "day" until 8 pm for that location. (And apply the same padding prior to sunrise.)


I wound up calculating the sun's altitude for the location using a UTC timestamp and the location's lat/long coordinates. Using figures for various twilight amounts (6, 12, 18 or degrees below horizon) I am able to automatically select the portion of "night" that is sufficiently dark. (e.g. 6 degrees below horizon is not as dark as 12 degrees.) This approach was much easier than attempting to determine sunrise and sunset times for the locale's time zone (and all of the associated time zone headaches).

share|cite|improve this question
up vote 5 down vote accepted

Formulas on that page are about as simple as you'll find and should meet your needs. If you're confused by something specific, just ask!

For shortening the period of night, you should go by the distance (in degrees, say) the Sun is below the horizion, rather than any fixed time. That is the setting of the "zenith" variable. You might like for background, then the values "official", "civil", "nautical", and "astronomical" on the page you referenced should make sense. Without even knowing the use for your sensor, I would suggest starting with the nautical twilight value, that is set the variable zenith to 102. (Nautical twilight is pretty dark!)

UPD: The math on the page looks good. I tried coding it and it gave the right answer (for my location today anyway) to within about a minute.

share|cite|improve this answer
Thanks for pointing out the usage of zenith, this may actually be more useful than creating a buffer, as it essentially means "at what point do we consider the sun to have set/risen." I have been using official (90°) thus far, and that's probably the reason I encounter problems -- it's not really dark yet! – JYelton Apr 5 '12 at 21:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.