Is there a way in Mathematica or Wolfram Alpha to get a plot of daylight hours vs calendar date and rate of change of daylight hours vs calendar date. Lets say I want the plot for latitude 45 degrees north?


1 Answer 1


As Brett's example that he linked to in his comment shows, this can be easily done using the Sunrise and Sunset functions:

days = DateRange[{2019, 1, 1}, {2019, 12, 31}, "Day"];
daylightHours = QuantityMagnitude@DateDifference[Sunrise[#], Sunset[#], "Hour"] & /@ days;
DateListPlot@Transpose[{days, daylightHours}]

Mathematica graphics

The rate of change can be approximated by:

DateListPlot@Transpose[{Most[days], Differences@daylightHours}]

Mathematica graphics

As a bonus, this page shows how to estimate the daylight time based on the sun's altitude, which is available through SunPosition:

gothenburg = Entity["City",{"Goteborg","VastraGotaland","Sweden"}];
latitude = gothenburg["Latitude"];
{azimuth,altitude} = SunPosition[gothenburg];

daysSinceVernalEquinox = QuantityMagnitude@DateDifference[
    DateObject[{2019, 3, 21}],

declination = Quantity[23.5 Sin[(daysSinceVernalEquinox/365) 365 Degree], "Degree"];
localHourAngle = ArcCos[-Tan[latitude] Tan[declination]]/Degree;
daylight = 2 Quantity[24, "Hours"] (localHourAngle/360)


The corresponding value obtained using Sunrise and Sunset:

DateDifference[Sunset[gothenburg], Sunrise[gothenburg], "Hour"]


  • $\begingroup$ That's fabulous, thank you. Note that in the 'daylightHours = " line it should be "Hour" ; "Hours" resulted in an error on my system running Mathematica 11.0.1. $\endgroup$
    – LA_Ross
    Sep 22, 2019 at 16:38
  • $\begingroup$ @LA_Ross Thanks, that's good to know. "Hours" works in Mathematica 12, but edited for better compatibility. $\endgroup$
    – C. E.
    Sep 22, 2019 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.