C# Sunrise/Sunset with latitude/longitude

Javascript calculations here. Now you just need to port.


Edit: the calculations are in the source code of this page now.


Edit: here is a direct link to the source code. No need to go hunting through the html.


I know this post is old, but in case anyone is still looking...

CoordinateSharp is available as a Nuget package. It's a standalone packge that can handle sun as well as moon times.

Celestial cel = Celestial.CalculateCelestialTimes(85.57682, -70.75678, new DateTime(2017,8,21));
Console.WriteLine(cel.SunRise.Value.ToString());

Note:

It assumes DateTimes are always in UTC.

Lastly, you may need to reference the Celestial objects Sun/Moon .Condition if a date returns null. This occurs when the sun is up/down all day.

EDIT 1/9/2019

The library has changed dramatically since this post. It can now handle local times as well.


I used NAA javascript and c# to create this library in C#.

Sunrise and Sunset in C#

I tested it against these two sites, and it shows time exactly like the sites do.

http://www.timeanddate.com/sun/usa/seattle

http://www.esrl.noaa.gov/gmd/grad/solcalc/