Click here to Skip to main content
15,499,155 members
Articles / Programming Languages / C# 4.0
Posted 4 May 2010


79 bookmarked

Solar Calculator - Calculate Sunrise, Sunset, and Maximum Solar Radiation

Rate me:
Please Sign up or sign in to vote.
4.79/5 (33 votes)
10 Jan 2011CPOL2 min read
A C# assembly for calculating Sunrise, Sunset, and Maximum Solar Radiation


I was looking for a .NET library to calculate the time the sun rises and sets. There were some libraries available, including here on The Code Project, but they had some problems. After trying some, I decided to create one myself. I needed the sunrise, sunset, and maximum solar radiation for a WordPress plug-in on my blog. The plug-in shows a map of the Netherlands together with the current weather conditions. The top of this post shows a screenshot of the plug-in. The map shows the real-time temperature including an icon. The icon is a translation of the measured solar radiation against the maximum solar radiation.


It is possible to calculate the sunrise, sunset, and maximum solar radiation using some known algorithms. For those of you who are interested in these algorithms, take a look at the following pages at Wikipedia: Declination of the Sun, Sunrise, Sunset. If you just want to calculate the sunrise, sunset, and maximum solar radiation, take a look below at how to use the code.

Using the Code

The code is packed in a Visual Studio 2008 solution. It contains two assemblies: Astronomy and AstronomyTest. The assembly Astronomy contains the SunCalculator class which performs the actual calculation. The assembly AstronomyTest contains several unit-tests that validate the calculations against external sources.

SunCalculator is a single class that does not depend on external classes. Although this somewhat goes against the Single Responsibility Principle, it makes reuse of this class easier. The SunCalculator class needs the longitude, latitude, and time-zone of your location. You should also indicate whether to use daylight savings. An instance of the SolarCalculator can be created like this:

const Double Longitute = 5.127869;
const Double Latitude = 52.108192;
const int LongituteTimeZone = 15;
const bool UseSummerTime = true;
SunCalculator sunCalculator = new SunCalculator(Longitute, Latitude, 
              LongituteTimeZone, UseSummerTime);

You have to supply the longitude and the latitude from the location that you want to calculate the sunrise and sunset time. These are the two first arguments of the constructor. For locations that use daylight savings, you should set UseSummerTime to the actual daylight savings status. For locations that don't use daylight savings, set it to false.

The actual calculation of sunrise, sunset, and maximum solar radiation can be seen below:

DateTime sunRise = sunCalculator.CalculateSunRise(new DateTime(2010, 4, 1));
DateTime sunSet = sunCalculator.CalculateSunSet(new DateTime(2010, 4, 1));
Double maximumSolarRadiation = 
  sunCalculator.CalculateMaximumSolarRadiation(new DateTime(2010, 1, 26, 16, 30, 0));

The DateTime that is returned from CalculateSunRise and CalculateSunSet includes the sunrise and sunset time. For more information, take a look at the unit-tests in the assembly AstronomyTest.

Points of Interest

The code first calculates the declination of the sun, cosine of the sun position, sinus of the sun position, and the difference between the solar and the actual time. All these parameters are used to calculate the sunrise and sunset times.

If you want to see the plug-in live, see my blog The data that is retrieved and shown on the map comes from, the company that I work for.


  • v1.0 02/04/2010: Initial and first release
  • v1.1 28/05/2010: Added a test case for Los Angeles, and a Console application that demonstrates the library in the source code
  • v1.2 08/11/2011: Fixed failing tests


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Netherlands Netherlands
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He works for SimpleTechture where he helps teams develop state of the art web applications.

Patrick enjoys writing his blog. It discusses agile software development. Patrick can be reached at

Published Windows 8 apps:

Published Windows Phone apps:


Best Mobile article of March 2012
Best Mobile article of June 2012

Comments and Discussions

QuestionMy vote of 1 Pin
quandary1-Sep-20 4:56
Memberquandary1-Sep-20 4:56 
QuestionCannot compile code Pin
hobbitdome2-Apr-19 6:52
Memberhobbitdome2-Apr-19 6:52 
QuestionError when sun does not rise in winter Pin
Member 1373061314-Jan-19 3:00
MemberMember 1373061314-Jan-19 3:00 
QuestionWhat is the value of longitudeTimeZone Pin
v8dave3-Nov-15 13:14
Memberv8dave3-Nov-15 13:14 
AnswerRe: What is the value of longitudeTimeZone Pin
Garth J Lancaster3-Nov-15 13:38
professionalGarth J Lancaster3-Nov-15 13:38 
QuestionWhy can't you calculate the longituteTimeZone parameter? Pin
hichaeretaqua18-May-14 9:07
Memberhichaeretaqua18-May-14 9:07 
AnswerRe: Why can't you calculate the longituteTimeZone parameter? Pin
Riaan Stander9-Oct-14 13:50
MemberRiaan Stander9-Oct-14 13:50 
GeneralRe: Why can't you calculate the longituteTimeZone parameter? Pin
RBinSFla19-Feb-15 8:40
MemberRBinSFla19-Feb-15 8:40 
GeneralMy vote of 5 Pin
ridoy9-May-13 10:53
professionalridoy9-May-13 10:53 
GeneralMy vote of 5 Pin
Sperneder Patrick30-Apr-13 5:02
professionalSperneder Patrick30-Apr-13 5:02 
GeneralRe: My vote of 5 Pin
Patrick Kalkman30-Apr-13 5:22
MemberPatrick Kalkman30-Apr-13 5:22 
QuestionHow to calcualte LongituteTimeZone? Pin
fortuna_621-Jan-13 9:14
Memberfortuna_621-Jan-13 9:14 
AnswerRe: How to calcualte LongituteTimeZone? Pin
Patrick Kalkman30-Apr-13 5:24
MemberPatrick Kalkman30-Apr-13 5:24 
Hi Boris,

The LongituteTimeZone is the nearest longitude in multiple of 15 of the timezone of which you are calculating the sun rise or sun set.

Hope this helps.
Patrick Kalkman

My latest article: Weekly Thai Recipe! for Windows Phone
My Blog:

GeneralMy vote of 4 Pin
Christian Amado23-Aug-12 12:54
professionalChristian Amado23-Aug-12 12:54 
GeneralRe: My vote of 4 Pin
Patrick Kalkman30-Apr-13 5:18
MemberPatrick Kalkman30-Apr-13 5:18 
QuestionHi Patrick: Pin
squizfloats17-Aug-12 13:53
Membersquizfloats17-Aug-12 13:53 
AnswerRe: Hi Patrick: Pin
Patrick Kalkman30-Apr-13 5:22
MemberPatrick Kalkman30-Apr-13 5:22 
QuestionSolar radiation Pin
Ruben Tacq3-Jul-12 22:11
MemberRuben Tacq3-Jul-12 22:11 
AnswerRe: Solar radiation Pin
Patrick Kalkman30-Apr-13 5:19
MemberPatrick Kalkman30-Apr-13 5:19 
QuestionWrong result Pin
madcat23-Mar-12 13:18
Membermadcat23-Mar-12 13:18 
AnswerRe: Wrong result Pin
Patrick Kalkman24-Mar-12 5:02
MemberPatrick Kalkman24-Mar-12 5:02 
QuestionHyperlink Failure Pin
Vasudevan Deepak Kumar29-Feb-12 5:34
MemberVasudevan Deepak Kumar29-Feb-12 5:34 
AnswerRe: Hyperlink Failure Pin
Patrick Kalkman24-Mar-12 5:04
MemberPatrick Kalkman24-Mar-12 5:04 
Questionwrong calculation Pin
Member 816554716-Aug-11 12:08
MemberMember 816554716-Aug-11 12:08 
AnswerRe: wrong calculation Pin
Patrick Kalkman24-Mar-12 5:06
MemberPatrick Kalkman24-Mar-12 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.