I am trying to calculate the point on Earth where the sun is directly overhead using javascript. My function for longitude works great, but I am having trouble with the latitude.
I understand that what I am looking for is called the Solar Declination, and an approximate formula for that is (source):
δ=23.45 * sin[(360/365)(284+N)]
where N is the day in the year, where January 1 is 1, Feb 1 is 32, etc.
This is the function I am using now:
function getSolarDeclination(){
return -23.44 * Math.sin( (360 / 365.25) * getDayOfYear() )
}
function getDayOfYear(){
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);
return dayOfYear + 284
}
However, this doesn't seem to be giving me the right number. For today, June 3, 2020, I am getting 17.607657911890687, whereas at least according to this site it should be ~22.465. This is several hundred miles off!
What am I doing wrong? Is this approximate formula just too "approximate"?
Looks like the formula you're using expects values in degrees, but Math.sin
uses radians. If you convert it, it gives roughly the expected result:
function getSolarDeclination(){
return -23.44 * Math.sin( (360 / 365.25) * getDayOfYear() * Math.PI/180 )
}
function getDayOfYear(){
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);
return dayOfYear + 284
}
console.log(getSolarDeclination())