Reputation: 11
I want to convert my Julian date number into normal date that is UTC date format in JavaScript . For an example I have Julian number "57115". I want to convert in format like 10 April,2015.
Upvotes: 1
Views: 3469
Reputation: 638
The well-known day numbering method that has a value 57,115 in April 2015 is the modified Julian date (MJD). It is the number of days since the midnight at the beginning of Gregorian calendar date November 17, 1858, Universal Time (UT). UT is the successor to the obsolete abbreviation GMT. MJD is always in UT. The time of day may be represented as a fraction of a day, so MJD 0.75 would be 18:00 Nov. 17, 1858, UT. I will ignore subtle differences of up to a second or so between UT and UTC since popular personal computing devices are only accurate to a few seconds at best. Here is an example of how to do the conversion, taking advantage of the built in Date object.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<BODY>
<pre>
<script language="JavaScript">
// Origin of Modified Julian Date (MJD) is 00:00 November 17, 1858 of
// the Gregorian calendar
// Internal JavaScript built in Date object internal time value 0
//milliseconds (ms) is 00:00 January 1, 1970, UT,
// equal to MJD 40587
// Internal value of MJD origion should be
//negative 40587 days * 86,400,000 milliseconds per day =
// 3,506,716,800,000 ms
var originMJD = -3506716800000;
document.write("Test creation of date object containing origin of MJD.");
document.write(new Date(-3506716800000).toUTCString());
document.writeln(" should be Wed, 17 Nov 1858 00:00:00 UT.");
//Given an MJD display the Gregorian calendar date and time.
var inMJD = 57115;
var inInternal = (inMJD * 86400000) + originMJD;
document.writeln();
document.writeln("Input MJD is ", inMJD, ".");
document.write("Equivalent Gregorian calendar date and time is ",
new Date(inInternal).toUTCString(), ".");
</script>
</pre>
</BODY>
</HTML>
The output of the example should be:
Test creation of date object containing origin of MJD.Wed, 17 Nov 1858 00:00:00 GMT should be Wed, 17 Nov 1858 00:00:00 UT.
Input MJD is 57115. Equivalent Gregorian calendar date and time is Fri, 03 Apr 2015 00:00:00 GMT.
Upvotes: 1
Reputation: 285
try the below code
var X = parseFloat(57115)+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);
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);
alert(new Date(Date.UTC(year, month, day, hour, minute, second)));
Upvotes: 1
Reputation: 3765
Here is the formula:
Q = JD+0.5
Z = Integer part of Q
W = (Z - 1867216.25)/36524.25
X = W/4
A = Z+1+W-X
B = A+1524
C = (B-122.1)/365.25
D = 365.25xC
E = (B-D)/30.6001
F = 30.6001xE
Day of month = B-D-F+(Q-Z)
Month = E-1 or E-13 (must get number less than or equal to 12)
Year = C-4715 (if Month is January or February) or C-4716 (otherwise)
Upvotes: 0