PDA

View Full Version : Julian Date format



makana
8 Jan 2011, 5:31 AM
Hi,

since I want to improve the SQLite functionality within the ext-air adapter, I got the request for a format and parse function for the julian date format (http://en.wikipedia.org/wiki/Julian_day) that is a common format in SQLite to store date and time values.

I want to share it here.


Ext.apply(Date.parseFunctions, {
"J": function(input, strict) {
/*var s = parseFloat(input);
s = (s - 2440587.5) * 86400;
return new Date(s * 1000);*/

var j = parseFloat(input),
y, m, d, h, i, s,
z, f, a, x, v, u, t;

j += 0.5;
z = Math.floor(j);

if (z < 2299161) {
a = z;
} else {
a = Math.floor((z - 1867216.25) / 36524.25);
a = z + 1 + a - Math.floor(a / 4);
}

x = a + 1524;
v = Math.floor((x - 122.1) / 365.25);
u = Math.floor(365.25 * v);
t = Math.floor((x - u) / 30.6001);

d = x - u - Math.floor(30.6001 * t) + j - z;
m = (t < 13.5) ? t - 1 : t - 13;
y = (m > 2.5) ? v - 4716 : v - 4715;

f = d - Math.floor(d);
d = Math.floor(d);

h = Math.floor(24 * f);
i = Math.floor(1440 * f - h / 24);
s = Math.round(86400 * (f - h / 24 - i / 1440));

return new Date(y, m - 1, d, h, i, s);
}
});
Ext.apply(Date.formatFunctions, {
"J": function() {
//return Math.round(this.getTime() / 1000) / 86400 + 2440587.5; // short form, but not exact
var d = this.getDate(),
m = this.getMonth() + 1,
y = this.getFullYear(),
h = this.getHours(),
i = this.getMinutes(),
s = this.getSeconds(),
x = 100 * y + m - 190002.5,
r = 367 * y;
r -= Math.floor(7 * (y + Math.floor((m + 9) / 12)) / 4);
r += Math.floor(275 * m / 9);
r += d;
r += (h + (i + s / 60) / 60) / 24;
r += 1721013.5;
r -= 0.5 * x / Math.abs(x);
r += 0.5;
return r;
}
});


There are short forms available that caltulate between unix timestamp format and julian date format (see commented parts), but they provide not exact results. (format differs already at the second decimal position, parse differs already in the hours). That's why the algorithm looks that complicated.

Just for completeness: If you want to use it, please consider to escape any "J" within the date formats you use currently in your app.

makana