To read the article online, visit

Determining the Sunrise and Sunset for a Particular Location

By Mike Shaffer

In my last article, Calculating the Distance Between Cities, I showed you how to calculate the distance between two geographical points on the Earth. In it, I jokingly mentioned that while you were scanning your database and performing all of the calculations for distance, you might want to entertain (or distract) the user by letting her know when the sun was going to set in her location (since by then you had her latitude and longitude).

Well, it's just my luck that people took me seriously and began sending emails asking how to perform sunrise and sunset calculations! :)

So, here is a snippet of code that will calculate either sunrise or sunset for a given latitude and longitude. There are all kinds of caveats attached to this code, and to avoid any misunderstandings, please read them carefully:

  • This code works for the years 1901 to 2099
  • This code will not work for latitudes greater than 63 degrees (North OR South)
  • This code calculates visual sunrise and sunset (this is what you normally see on the evening news). It is NOT intended to be used for other types of sunrise/sunset events such as civil, nautical or astronomical twilight (although it could be modified to do so)
  • This code is not intended to produce results that adhere to any religious definitions of sunrise/sunset
  • This code does NOT take regional time changes (e.g. Daylight Savings time) into account.

Whew! Having now examined all of the disclaimers, let's look at the parameters required to call the GetSunRiseSet function:

year, month, day
year/month/day to use for calculations

latitude of observer

longitude of observer

This is the offset from GMT for the observer

This is "R" for sunrise (default), or "S" for sunset

Here is some sample code that shows how to call the GetSunRiseSet function:

y = year(now) m = month(now) d = day(now) ' Set these to the latitude/longitude of the observer MyLatitude = 32.9697 MyLongitude = -96.80322 ' Set this to your offset from GMT (e.g. for Dallas is -6) ' NOTE: The routine does NOT handle switches to/from daylight savings ' time, so beware! MyTimeZone = -6 ' Note:Set RiseOrSet to "R" for sunrise, "S" for sunset RiseOrSet = "R" ttt = GetSunRiseSet(MyLatitude, MyLongitude, MyTimeZone, _ RiseOrSet, y, m, d) response.write "<html><body>Sunrise for Dallas on " response.write datevalue(now) response.write " occurs at: " & ttt & " (24-hour time)</body></html>"

  • Run the above code snippet...

    The result returned from the GetSunRiseSet function will be the time of the event (in 24-hour or military-time format).

    Yes, there are alternatives to using this code. One thing that I have seen many sites do is to perform a 'screen-scrape' from another site that already performs these calculations, such as While that technique may work in general, it relies on the fact that is always up when you need it. When it comes to solving programming problems, I believe that 'elegant and reliable' solutions are much better than 'quick-and-dirty' solutions. (As a side note, you can learn how to grab information off of other web servers by reading, Grabbing Information From Other Servers.)

    It should be noted that this code is based on the work of several other people, most of whom specialize in the field of celestial mechanics. While it's true that I am an avid amateur astronomer, I cannot claim credit for developing the methods that are encapsulated by these routines. Please see the comments in the routines for the names of the people whose shoulders I was fortunate to be able to stand upon.

    For information on how to obtain a database of cities, zip codes, and longitude/latitude measurements, be sure to consult my previous article, or visit

    Happy Programming!


  • Download the source code in text format
  • Test the code!

  • Article Information
    Article Title: Determining the Sunrise and Sunset for a Particular Location
    Article Author: Mike Shaffer
    Published Date: Saturday, January 08, 2000
    Article URL:

    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers