Convert Date to and back Julian Date - Node.js Date

Node.js examples for Date:Date Calculation

Description

Convert Date to and back Julian Date

Demo Code


Date.prototype.Date2Julian = function() {
  return Math.floor( ( this / 86400000 ) - ( this.getTimezoneOffset() / 1440 ) + 2440587.5 );
};

Number.prototype.Julian2Date = function() {

  var X = parseFloat(this)+0.5;
  var Z = Math.floor(X); //Get day without time
  var F = X - Z; //Get time
  var Y = Math.floor((Z-1867216.25)/36524.25);
  var A = Z+1+Y-Math.floor(Y/4);
  var B = A+1524;
  var C = Math.floor((B-122.1)/365.25);
  var D = Math.floor(365.25*C);
  var G = Math.floor((B-D)/30.6001);

  //must get number less than or equal to 12)
  var month = (G < 13.5) ? ( G - 1 ) : ( G - 13 );

  //if Month is January or February, or the rest of year
  var year = (month<2.5) ? (C-4715) : (C-4716);
  month -= 1; //Handle JavaScript month format
  var UT = B-D-Math.floor(30.6001*G)+F;
  var day = Math.floor(UT);

  //Determine time
  UT -= Math.floor(UT);//from  w  ww .  j  av  a 2 s.co  m
  UT *= 24;
  var hour = Math.floor(UT);
  UT -= Math.floor(UT);
  UT *= 60;
  var minute = Math.floor(UT);
  UT -= Math.floor(UT);
  UT *= 60;
  var second = Math.round(UT);

  return new Date(Date.UTC(year, month, day, hour, minute, second));
};

Related Tutorials