I am trying to convert a Julian integer back to a date.
Example:
integer 2456931 = 2014/09/30 // format YYYY/MM/DD
Please Help - Thanks in Advance,
Here is what I developed for my own need :
function dateToJulianNumber(d) {
// convert a Gregorian Date to a Julian number.
// S.Boisseau / BubblingApp.com / 2014
var x = Math.floor((14 - d.getMonth())/12);
var y = d.getFullYear() + 4800 - x;
var z = d.getMonth() - 3 + 12 * x;
var n = d.getDate() + Math.floor(((153 * z) + 2)/5) + (365 * y) + Math.floor(y/4) + Math.floor(y/400) - Math.floor(y/100) - 32045;
return n;
}
// assert September 30 2014 -> 2456931
console.log(dateToJulianNumber(new Date(2014,9,30)).toString());
function julianIntToDate(n) {
// convert a Julian number to a Gregorian Date.
// S.Boisseau / BubblingApp.com / 2014
var a = n + 32044;
var b = Math.floor(((4*a) + 3)/146097);
var c = a - Math.floor((146097*b)/4);
var d = Math.floor(((4*c) + 3)/1461);
var e = c - Math.floor((1461 * d)/4);
var f = Math.floor(((5*e) + 2)/153);
var D = e + 1 - Math.floor(((153*f) + 2)/5);
var M = f + 3 - 12 - Math.round(f/10);
var Y = (100*b) + d - 4800 + Math.floor(f/10);
return new Date(Y,M,D);
}
// assert 2456931 -> September 30 2014
console.log(julianIntToDate(2456931).toString());
Here is a sample HTML file that illustrates using the Date Java built-in object.
<!DOCTYPE html>
<html>
<body>
<h1>Julian date conversion example</h1>
<script>
var jd = 2456931;
var millis = (jd - 2440587.5) * 86400000;
var dateLocal = new Date(millis);
document.writeln(dateLocal);
document.writeln("<br>");
document.write(dateLocal.getUTCFullYear(), "-");
document.write(dateLocal.getUTCMonth()+1, "-");
document.write(dateLocal.getUTCDate(), "T12:00:00Z");
</script>
</body>
</html>
The idea is to subtract the Julian date of midnight at the beginning of January 1, 1970, UTC, which is the beginning of the millisecond count used by JavaScript Date objects. Then the number of days since January 1, 1970, is multiplied by 86,400,000, the number of milliseconds in a day.
The millisecond value is used to create a new Date object.
The results, displayed in a computer with the time zone set to eastern US with daylight saving time in effect:
Tue Sep 30 2014 08:00:00 GMT-0400 (Eastern Standard Time)
2014-9-30T12:00:00Z
Note that I wrote the UTC date and time in a format similar to ISO 8601, but I didn't bother to pad the date or month when the value is < 10.
In case you weren't aware, Julian dates always begin at noon UTC.
The above does not work for me. The following solution is coded strictly following the wikipedia page ( wikipedia) -
function dateToJulianNumber0(d){
var year=d.getFullYear();
var month=d.getMonth()+1;
var day=d.getDate();
var a = Math.floor((14-month)/12);
var y = Math.floor(year+4800-a);
var m = month+12*a-3;
var JDN = day + Math.floor((153*m+2)/5)+(365*y)+Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400)-32045;
return JDN;
}
function julianIntToDate0(JD){
var y = 4716;
var v = 3;
var j = 1401;
var u = 5;
var m = 2;
var s = 153;
var n = 12;
var w = 2;
var r = 4;
var B = 274277;
var p = 1461;
var C = -38;
var f = JD + j + Math.floor((Math.floor((4 * JD + B) / 146097) * 3) / 4) + C;
var e = r * f + v;
var g = Math.floor((e % p) / r);
var h = u * g + w;
var D = Math.floor((h % s) / u) + 1;
var M = ((Math.floor(h / s) + m) % n) + 1;
var Y = Math.floor(e / p) - y + Math.floor((n + m - M) / n) ;
return new Date(Y,M-1,D);
}
//Testing
var jd=dateToJulianNumber0(new Date(2013,11,31)); //Month is 0-based for javascript
var gd=julianIntToDate0(jd);
console.log(jd);
console.log(gd.toString());
A convenient way to script JD/JDN is to calculate from a Date object's internal time value. Assuming, like other solutions here, you're using a UTC Julian Period epoch and JavaScript's way of accurately reporting UTC time while ignoring leap seconds in getTime()
, these work:
function jd(dateObj) { // Decimal days
return dateObj / 86400000 + 2440587.5;
}
function jdn(dateObj) { // Integer days (advances at noon)
return Math.floor(dateObj / 86400000 + 2440587.5);
}
function jdUTC(Y, M, D, H, m, s, ms) { // M is Jan = 0, Feb = 1, etc.
// Add local hour offset to `H` or minute offset to `m` for local time
return Date.UTC.apply(Date, arguments) / 86400000 + 2440587.5;
}
function dateFromJD(jd, isValue) { // Any time of day to nearest millisecond
var obj = new Date();
obj.getJD = Date_getJD;
obj.setJD = Date_setJD;
if (arguments.length) obj.setJD(jd);
if (isValue) obj.valueOf = Date_getJD;
return obj;
}
function Date_setJD(jd) {
this.setTime(Math.round((jd - 2440587.5) * 86400000));
return this;
}
function Date_getJD() {
return this.getTime() / 86400000 + 2440587.5;
}
If you're using Date objects anyway, these are faster than long math calculations. But if you don't otherwise have use for Date objects, pure math can be faster (jsperf.co):
If it isn't too late - for Nodejs I have used moment js module and it worked as expected for me-
const moment = require("moment");
let date = moment("2456931", "YYYYDDD").format("YYYY/MM/DD");
console.log(date);
function dateToJulianNumber(d) {
// convert a Gregorian Date to a Julian number.
// S.Boisseau / BubblingApp.com / 2014
var x = Math.floor((14 - d.getMonth())/12);
var y = d.getFullYear() + 4800 - x;
var z = d.getMonth() - 3 + 12 * x;
var n = d.getDate() + Math.floor(((153 * z) + 2)/5) + (365 * y) + Math.floor(y/4) + Math.floor(y/400) - Math.floor(y/100) - 32045;
return n;
}
// assert September 30 2014 -> 2456931
console.log(dateToJulianNumber(new Date(2014,1,1)).toString());
function julianIntToDate(n) {
// convert a Julian number to a Gregorian Date.
// S.Boisseau / BubblingApp.com / 2014
var a = n + 32044;
var b = Math.floor(((4*a) + 3)/146097);
var c = a - Math.floor((146097*b)/4);
var d = Math.floor(((4*c) + 3)/1461);
var e = c - Math.floor((1461 * d)/4);
var f = Math.floor(((5*e) + 2)/153);
var D = e + 1 - Math.floor(((153*f) + 2)/5);
var M = f + 3 - 12 - Math.round(f/10);
var Y = (100*b) + d - 4800 + Math.floor(f/10);
return new Date(Y,M,D);
}
// assert 2456931 -> September 30 2014
console.log(julianIntToDate(2456931).toString());
来源:https://stackoverflow.com/questions/26370688/convert-a-julian-date-to-regular-date-in-javascript