-
Notifications
You must be signed in to change notification settings - Fork 0
/
julian.html
231 lines (198 loc) · 5.86 KB
/
julian.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html>
<head>
<title>Julian Dates</title>
<link rel="stylesheet" href="default.css">
<link rel="stylesheet" href="highlight/styles/default.css">
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body onload="calculateAll();">
<script src="highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<h1>Julian Date Algorithms</h1>
These two functions below are short versions that work with the time elapsed in milliseconds since Jan 1, 1970 UTC.
Most languages have routines to produce time in this way, so it may be preferable to the long routine below. Since
almost all libraries have issues dealing with dates before Oct 15, 1582, be very careful trying to modify these
routines to work with dates before then. If you have the time in seconds, rather than milliseconds, change
86400000 to 86400.
<pre>
<code class="JavaScript" id='code1'>
function JulianDateFromUnixTime(t){
//Not valid for dates before Oct 15, 1582
return (t / 86400000) + 2440587.5;
}
function UnixTimeFromJulianDate(jd){
//Not valid for dates before Oct 15, 1582
return (jd-2440587.5)*86400000;
}
</code>
</pre>
JavaScript functions to convert to and from a Julian Date. Algorithms from Astonomical Algorithms (Meeus).
<div style='border-style: solid; border-width:1;'>
<h3>Convert Gregorian Date to Julian Date:</h3>
<form>
<input value="2017" size=4 type=text id='inputYear'> -
<input value="08" size=2 type=text id='inputMonth'> -
<input value="21" size=2 type=text id='inputDay'>
<input value="18" size=2 type=text id='inputHour'> :
<input value="25" size=2 type=text id='inputMin'> :
<input value="32" size=2 type=text id='inputSec'>
<input type=button value="Calculate" onclick="calculate1();"><br>
Julian Date: <input type=text id="outputJD">
</form>
</div>
<p></p>
<div style='border-style: solid; border-width:1;'>
<h3>Convert Julian Date to Gregorian Date:</h3>
<form>
Julian Date: <input type=text id="inputJD" value="2457987.267731481">
<input type=button value="Calculate" onclick="calculate2();"><br>
<input size=4 type=text id='outputYear'> -
<input size=2 type=text id='outputMonth'> -
<input size=2 type=text id='outputDay'>
<input size=2 type=text id='outputHour'> :
<input size=2 type=text id='outputMin'> :
<input size=2 type=text id='outputSec'>
</form>
</div>
<script>
function calculateAll(){
calculate1();
calculate2();
}
function calculate1(){
let year=document.getElementById("inputYear").value-0;
let month=document.getElementById("inputMonth").value-0;
let day=document.getElementById("inputDay").value-0;
let hour=document.getElementById("inputHour").value-0;
let min=document.getElementById("inputMin").value-0;
let sec=document.getElementById("inputSec").value-0;
document.getElementById("outputJD").value=gregorianDateToJulianDate(year,month,day,hour,min,sec);
}
function calculate2(){
data=julainDateToGregorian(document.getElementById("inputJD").value-0);
document.getElementById("outputYear").value=data[0];
document.getElementById("outputMonth").value=data[1];
document.getElementById("outputDay").value=data[2];
document.getElementById("outputHour").value=data[3];
document.getElementById("outputMin").value=data[4];
document.getElementById("outputSec").value=data[5];
}
</script>
<script id='source1'>
//Special "Math.floor()" function used by dateToJulianDate()
function INT(d){
if(d>0){
return Math.floor(d);
}
if(d==Math.floor(d)){
return d;
}
return Math.floor(d)-1;
}
function gregorianDateToJulianDate(year, month, day, hour, min, sec){
let isGregorian=true;
if(year<1582 || (year == 1582 && (month < 10 || (month==10 && day < 5)))){
isGregorian=false;
}
if (month < 3){
year = year - 1;
month = month + 12;
}
let b = 0;
if (isGregorian){
let a = INT(year / 100.0);
b = 2 - a + INT(a / 4.0);
}
let jd=INT(365.25 * (year + 4716)) + INT(30.6001 * (month + 1)) + day + b - 1524.5;
jd+=hour/24.0;
jd+=min/24.0/60.0;
jd+=sec/24.0/60.0/60.0;
return jd;
}
//From Meeus, CH7
function julainDateToGregorian(jd){
let temp=jd+.5;
let Z=Math.trunc(temp);
let F=temp-Z;
let A=Z;
if(Z>=2299161){
let alpha=INT((Z-1867216.25)/36524.25);
A=Z+1+alpha-INT(alpha/4);
}
let B=A+1524;
let C=INT((B-122.1)/365.25);
let D=INT(365.25*C);
let E=INT((B-D)/30.6001);
let day=B-D-INT(30.6001*E)+F;
let month=E-1;
if(E>13){
month=E-13;
}
let year=C-4716;
if(month<3){
year=C-4715;
}
return [year,month,day,0,0,0];
}
</script>
<pre><code class="JavaScript" id='code1'>
//Special "Math.floor()" function used by dateToJulianDate()
function INT(d){
if(d>0){
return Math.floor(d);
}
if(d==Math.floor(d)){
return d;
}
return Math.floor(d)-1;
}
function gregorianDateToJulianDate(year, month, day, hour, min, sec){
let isGregorian=true;
if(year<1582 || (year == 1582 && (month < 10 || (month==10 && day < 5)))){
isGregorian=false;
}
if (month < 3){
year = year - 1;
month = month + 12;
}
let b = 0;
if (isGregorian){
let a = INT(year / 100.0);
b = 2 - a + INT(a / 4.0);
}
let jd=INT(365.25 * (year + 4716)) + INT(30.6001 * (month + 1)) + day + b - 1524.5;
jd+=hour/24.0;
jd+=min/24.0/60.0;
jd+=sec/24.0/60.0/60.0;
return jd;
}
//From Meeus, CH7
function julainDateToGregorian(jd){
let temp=jd+.5;
let Z=Math.trunc(temp);
let F=temp-Z;
let A=Z;
if(Z>=2299161){
let alpha=INT((Z-1867216.25)/36524.25);
A=Z+1+alpha-INT(alpha/4);
}
let B=A+1524;
let C=INT((B-122.1)/365.25);
let D=INT(365.25*C);
let E=INT((B-D)/30.6001);
let day=B-D-INT(30.6001*E)+F;
let month=E-1;
if(E>13){
month=E-13;
}
let year=C-4716;
if(month<3){
year=C-4715;
}
return [year,month,day,0,0,0];
}
</code>
</pre>
</body>
</html>