Author Topic: Simple (?) formula to calculate hous of daylight for a given DayOfYear and lat  (Read 388 times)

0 Members and 1 Guest are viewing this topic.

Offline DaleReid

  • Forecaster
  • *****
  • Posts: 2059
    • Weather at Eau Claire, WI
I have a need to get a number, once a day, for the hours of daylight for that Day Of Year and the Latitude.

A couple of internet searches which could be  used are a bit more calculation intensive (although it would only have to be done once a day for the whole day) which work by calculating the sunrise and sunset times and doing the subtraction.  For those who've done this, there are a few steps and involve a bit of the trig functions of a compiler.

I was hoping there might be something somewhat more simplistic, such as a function which could be written which would accept DayOfYear and Latitude and return a number of hours to a couple decimal places.

Has anyone heard of or seen such a simplified function, or should I just code the sunset - sunrise = hours solution?
Dale
ECWx.info
&
ECWx.info/t/index.php

Offline davidmc36

  • He who dies with the most toys wins!
  • Forecaster
  • *****
  • Posts: 1505
  • FN25IE61IX
    • MorewoodW34
Too bad the Sun Recorder software for the BL Sun Recorder will not run without the dongle. It has a built in calculator. Put in location and it does the rest.

Offline davidmc36

  • He who dies with the most toys wins!
  • Forecaster
  • *****
  • Posts: 1505
  • FN25IE61IX
    • MorewoodW34
This gives you a table for whole year including total time each day:

https://nrc.canada.ca/en/research-development/products-services/software-applications/sun-calculator/

I did it for my location. I don't know if it works for locations outside of Canada.

Offline davidmc36

  • He who dies with the most toys wins!
  • Forecaster
  • *****
  • Posts: 1505
  • FN25IE61IX
    • MorewoodW34
Seems to work for your location. Gives all dates in 2025. Or you can do single days.
« Last Edit: June 18, 2025, 09:41:48 AM by davidmc36 »

Offline DaleReid

  • Forecaster
  • *****
  • Posts: 2059
    • Weather at Eau Claire, WI
Thanks, David.
I should have been more explicit.  I have found similar online calculators for doing the heavy lifting, too.   With the solar industry push, a lot of them were on line associated with evaluating  how much benefit one might  get from putting  up panels.  I think there was a government site that gave that info, too, and references to an ephemeris that the Naval Observatory provided.

My need is for the actual formulas for doing the calculations, since this will be encoded in Campbell Scientific CRBasic running on a datalogger and storing the number in a stand alone program.

Fascinating how these things are done when you look at the process.
Dale
ECWx.info
&
ECWx.info/t/index.php

Offline mcrossley

  • Forecaster
  • *****
  • Posts: 1281
    • Wilmslow Astro
It you want it accurate there is no "simple" function, you have to do all the astro trig to work out rise and set times, then find the difference. Each year is slightly different.
Mark

Offline mcrossley

  • Forecaster
  • *****
  • Posts: 1281
    • Wilmslow Astro
I should add that there are some different astro functions for finding the rise/set times. The simpler ones being less accurate (they use approximations and/or leave it less influential terms), you have to decide what degree of accuracy you require.
Mark

Offline BoDuke

  • Global Moderator
  • Senior Contributor
  • *****
  • Posts: 159
  • Ambient Weather, Ecowitt, WeeWX, Custom PHP
I decided to see if AI (copilot) could help with this.  Here's what it came up with for a formula to estimate daylight hours (H) using latitude (L) in degrees and day of the year (D):

H = 24 − (24/pi) × arccos [−tan(L × pi/180) × tan(0.4091 × sin(2pi × (D − 81) / 365))]

It's not exactly simple but might be compared to the more accurate formulas, if it even works.  I haven't checked it to see if it's even in the right ballpark.  Some more notes from copilot:

Parameters:
    L = latitude in degrees (positive for Northern Hemisphere, negative for Southern)
    D = day number of the year (e.g. Jan 1 = 1, Jun 21 ≈ 172, Dec 31 = 365)
    The constant 0.4091 radians ≈ 23.44°, which is Earth's axial tilt
    All trigonometric functions operate in radians

This formula wraps everything together—converts latitude into radians, calculates the seasonal variation of solar declination, and estimates the daylight hours accordingly.

Keep in mind:
    It works great from roughly 66°S to 66°N (tropics to subarctic)
    Near or within the polar circles, accuracy breaks down due to polar day/night effects

If the formula works, hopefully it can be expressed into CRBasic code.

Offline DaleReid

  • Forecaster
  • *****
  • Posts: 2059
    • Weather at Eau Claire, WI
#1-  Wow, thanks
#2 -  to me, this is a very simple formula, and basically boils down to a one line chunk of code
#3 -  I will do a bit of testing with it over the next day or so.

Interesting to see how it approaches the solution.  I am pretty sure that the trig functions are available in CRBasic, but will see.

Now I have to see what copilot is (not a term I'm used to).  I wonder if  you would tell us what search criteria  you used, and when the results  you shared came up, was it basically a web page that the search found, or how  you got to this.  It would have saved a bunch of postings, but on the other hand it illuminates, no pun, a lot.
Dale
ECWx.info
&
ECWx.info/t/index.php

Offline BoDuke

  • Global Moderator
  • Senior Contributor
  • *****
  • Posts: 159
  • Ambient Weather, Ecowitt, WeeWX, Custom PHP
Hi Dale,

Copilot is Microsoft's AI assistant at copilot.microsoft.com that you can use without even creating an account or signing in, although those are possible which would allow you to save your conversation history.

While the media raves over all the AI implementations (ChatGPT, Gemini, Copilot, etc.), they are far from perfect or even trustworthy.  You have to treat them as an artificial assistant that may tell you the truth or complete fiction.  That said, they can be very useful to summarize or find things.  I think of them as overconfident, mistake-prone robots that have a vast wealth of knowledge.  If you point out a mistake, they'll say they are sorry but they may repeat that same, or similar, mistake again very soon.  They don't really, truly learn from the conversation.

I started the conversation with Copilot by asking if there was a simple formula for calculating the number of daylight hours for a location, given only the latitude in degrees and the day number of the date.  Then I asked follow up questions to combine a couple of the answers into a single formula.  If you ask different ways or start new conversations, it is possible that you could end up with different answers, so be aware of the pros/cons of that.  You could also even ask it to write you the CRBasic code but don't be surprised if it doesn't work or is different from the actual formula.  It could be a good way to get a head start and, if you're lucky, you might have success after going back and forth.

Good luck!

Offline DaleReid

  • Forecaster
  • *****
  • Posts: 2059
    • Weather at Eau Claire, WI
Thank  you, I am learning every day.

I appreciate your warning of caution on the validity of what comes out.

My wife has read about that program and mentioned it is highly dependent on HOW you ask the question, and your response was very  helpful  I will play with it more later.

The first pass on the formula that was provided gives a good, close enough, estimate in the one by-hand calculation I ran.
Dale
ECWx.info
&
ECWx.info/t/index.php

Offline BoDuke

  • Global Moderator
  • Senior Contributor
  • *****
  • Posts: 159
  • Ambient Weather, Ecowitt, WeeWX, Custom PHP
In my Copilot conversation, it even offered to provide a chart of sample dates and the actual versus formula-estimated hours of daylight.  I took it up on the offer and it did vary by at least a half hour at one extreme, probably around the shortest day.  That's another thing AI can excel at - doing the menial, busy work like making a table of values.  You just have to second-guess/double-check it.

Helping each other learn and solve problems is one of the primary purposes of this forum, so I'm glad I was able to help.
UU

 

anything