I am trying to calculate the Julian Date from a calendar date string using javascript. I found this site that explains how to calculate it.
I made this javascript code: https://jsfiddle.net/kzgn8vs1/1/
var time = moment("2018-04-07 23:45:16 -0300", "YYYY-MM-DD HH:mm:ss Z").utc();
var Y = time.year();
var M = time.month() + 1;
var D = time.date() + moment.duration(time.format('HH:mm:ss')).asDays();
if( M == 1 || M == 2 ) {
Y = Y - 1;
M = M + 12;
}
var A = Y / 100;
var B = 2 - A + (A / 4);
var C = ( Y > 0 ) ? (365.25 * Y) : ((365.25 * Y) - 0.75);
var E = 30.6001 * (M + 1);
var JD = B+C+D+E+1720994.5;
console.log(JD);
Which returns 2458216.9802685184 for the date 2018-04-07 23:45:16 -0300. But when I try some online converters like http://www.onlineconversion.com/julian_date.htm and https://www.aavso.org/jd-calculator
Entering the date 2018-04-08 2:45:16 (my date converted to UTC), both return 2458216.61477. A 0.36 difference from my result.
Where is the error? In my code or in the calculation method?
Based on the StackOverflow Question here, I have modified your JsFiddle
I have changed your implementation based on
2440587.5 days + UNIX TIME in days === Julian Day
(UNIX TIME / 86400000) + 2440587.5) === Julian Day;
and have updated your code to reflect as var JD = time/86400000 + 2440587.5;
To get the same result as the online calculator I had to use the UTC time you used with the online examples.