Julian dates

by toubia95

CSS

/* http://en.wikipedia.org/wiki/Conversion_between_Julian_and_Gregorian_calendars */
/* LE SCRIPT EST OK */
/*cf http://calendarhome.com/calculate/convert-a-date/ */

JavaScript

//  GREGORIAN_TO_JD  --  Determine Julian day number from Gregorian calendar date

var GREGORIAN_EPOCH = 1721425.5;

//  LEAP_GREGORIAN  --  Is a given year in the Gregorian calendar a leap year ?

function leap_gregorian(year) {
    return ((year % 4) === 0) && (!(((year % 100) === 0) && ((year % 400) !== 0)));
}

function gregorian_to_jd(year, month, day) {
    return (GREGORIAN_EPOCH - 1) + (365 * (year - 1)) + Math.floor((year - 1) / 4) + (-Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12) + ((month <= 2) ? 0 : (leap_gregorian(year) ? -1 : -2)) + day);
}

//  JD_TO_JULIAN  --  Calculate Julian calendar date from Julian day

function jd_to_julian(td) {
    var z, a, b, c, d, e, year, month, day;

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

    a = z;
    b = a + 1524;
    c = Math.floor((b - 122.1) / 365.25);
    d = Math.floor(365.25 * c);
    e = Math.floor((b - d) / 30.6001);

    month = Math.floor((e < 14) ? (e - 1) : (e - 13));
    year = Math.floor((month > 2) ? (c - 4716) : (c - 4715));
    day = b - d - Math.floor(30.6001 * e);

    /*  If year is less than 1, subtract one to convert from
        a zero based date system to the common era system in
        which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.).  */

    if (year < 1) {
        year -= 1; //year--;
    }

    return [year, month, day];
}

//  JWDAY  --  Calculate day of week from Julian day

var Weekdays = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday");

function jwday(j) {
    return mod(Math.floor((j + 1.5)), 7);
}

/*  MOD  --  Modulus function which works for non-integers.  */

function mod(a, b) {
    return a - (b * Math.floor(a / b));
}

var date_julienne = gregorian_to_jd(1972, 7, 8);
console.log("datejul = " + date_julienne);
var cal_julien = jd_to_julian(date_julienne);
console.log("caljul = " + cal_julien);
weekday = jwday(date_julienne);
console.log("weekday = " +...