# Daylight hours vs calendar date

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?

• – Moo
Sep 21, 2019 at 15:34
• should also have wolfram-alpha-query tag but I don't know how to add an additional tag. Sep 22, 2019 at 0:54
• Here's how I did it: wolfram.com/language/12/core-visualization/… Sep 22, 2019 at 2:58

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}]

The rate of change can be approximated by:

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

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}],
DateObject[],
"Days"
];

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

11.5259

The corresponding value obtained using Sunrise and Sunset:

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

11.7667

• 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. Sep 22, 2019 at 16:38
• @LA_Ross Thanks, that's good to know. "Hours" works in Mathematica 12, but edited for better compatibility. Sep 22, 2019 at 17:05