When you think ASP, think...
 Recent Articles All Articles ASP.NET Articles ASPFAQs.com Related Web Technologies User Tips! Coding Tips

 Sections: Sample Chapters JavaScript Tutorials MSDN Communities Hub Official Docs Security Stump the SQL Guru! XML Info Information: Feedback Author an Article
Published: Saturday, January 08, 2000

# 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).

- continued -

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` latitude of observer `Longitude` longitude of observer `ZoneRelativeGMT` This is the offset from GMT for the observer `RiseOrSet` 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 "Sunrise for Dallas on " response.write datevalue(now) response.write " occurs at: " & ttt & " (24-hour time)" ```

• 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 www.weather.com. While that technique may work in general, it relies on the fact that weather.com 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 GreatData.com.

Happy Programming!

Attachments:

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

•  ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article

 #brand-footer{text-align:center;margin:0 auto;} #commonfooterpropertytext{padding-bottom: 12px;} #commonfootercopyright{padding: 3px;} #eweekBrand-footer{text-align:center;margin:0 auto} #footerDisclaimerText{margin: 15px 10px 0px 10px; text-align: justify;} Advertiser Disclosure: Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.