File Exchange

image thumbnail

sunRiseSet( lat, lng, UTCoff, date, PLOT)

version (4.59 KB) by Richard Droste
Compute accurate apparent sunrise, sunset and noon times in seconds, and solar angles


Updated 06 Nov 2017

View Version History

View License

Compute refraction-corrected sunrise, sunset and noon times in seconds at a given latitude, longitude and date.
Compute solar elevation, azimuthal and declination angles of the sun at every second of the day.

The code is also hosted on

sun_rise_set = sunRiseSet( lat, lng, UTCoff, date) Computes the *apparent* (refraction
corrected) sunrise and sunset times in seconds from mignight and returns them as
sun_rise_set. lat and lng are the latitude (+ to N) and longitude (+ to E), UTCoff is the
timezone, i.e. the local time offset to UTC (Coordinated Universal Time) in hours, and date is
the date in format 'dd-mmm-yyyy' ( see below for an example).

[sun_rise_set, noon] = sunRiseSet( lat, lng, UTCoff, date) additionally returns the solar noon
in seconds from midnight.

[sun_rise_set, noon, opt] = sunRiseSet( lat, lng, UTCoff, date) additionally returns the
information opt, which contains information on every second of the day:
opt.elev_ang_corr : Apparent (refraction corrected) solar elevation in degrees
opt.azmt_ang : Solar azimuthal angle (deg cw from N)
opt.solar_decl : Solar declination in degrees

sun_rise_set = sunRiseSet( ..., PLOT) If PLOT is true, plots of the elevation and azimuthal
angle are created.

lat = 47.377037; % Latitude (Zurich, CH)
lng = 8.553952; % Longitude (Zurich, CH)
UTCoff = 2; % UTC offset
date = '15-jun-2017';

[sun_rise_set, noon, opt] = sunRiseSet( lat, lng, UTCoff, date, 1);

Richard Droste

Reverse engineered from the NOAA Excel:

The formulas are from:
Meeus, Jean H. Astronomical algorithms. Willmann-Bell, Incorporated, 1991.

Cite As

Richard Droste (2021). sunRiseSet( lat, lng, UTCoff, date, PLOT) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)


Mike Eichhorn

Daniel Pascual

François Beauducel

William Moutier

Hi, Works well except when the sunset or the sunrise happens the day before or after the selected day. For example when the sunset happens the day before, the code provides a value of 00:00:01 which is not correct. Does somebody know how to fix that?



Works well after I figure out what UTC was. It would be helpful to include a better description in the notes section for UTC. Have you considered adding a function to programmatically determine the UTC based off the lng, lat inputs?

I do not have the financial toolbox and was not able to use daysact() . A simple fix was to replace Line 39 with:
nDays = datenum(date)-datenum('30-dec-1899');

Also, the lines of code with trigonometric calculations can be cleaned up by using sind(), asind(), etc. functions versus rad2deg(asin(deg2rad(sin(value)))).

Camilo Márquez

it's possible use date option like a vector?. I need calculate sunset/sunrise varations troughtout the year.

Kelvin Prado


Dave Holler

Very neat!

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: SOLAR_GUI

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!