title: Calculating julian date with javascript
body: <p>I am trying to calculate the Julian Date from a calendar date string using javascript. I found <a href="http://129.79.46.40/~foxd/cdrom/musings/formulas/formulas.htm" rel="nofollow noreferrer">this site</a> that explains how to calculate it.</p>
  
  <p>I made this javascript code: <a href="https://jsfiddle.net/kzgn8vs1/1/" rel="nofollow noreferrer">https://jsfiddle.net/kzgn8vs1/1/</a> </p>
  
  <pre><code>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 &gt; 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);
  </code></pre>
  
  <p>Which returns <strong>2458216.9802685184</strong> for the date 2018-04-07 23:45:16 -0300. But when I try some online converters like <a href="http://www.onlineconversion.com/julian_date.htm" rel="nofollow noreferrer">http://www.onlineconversion.com/julian_date.htm</a> and <a href="https://www.aavso.org/jd-calculator" rel="nofollow noreferrer">https://www.aavso.org/jd-calculator</a></p>
  
  <p>Entering the date 2018-04-08 2:45:16 (my date converted to UTC), both return <strong>2458216.61477</strong>. A 0.36 difference from my result.</p>
  
  <p>Where is the error? In my code or in the calculation method?</p>