Показать сообщение отдельно
  #51 (permalink)  
Старый 23.11.2019, 04:51
Аватар для Блондинка
Профессор
Отправить личное сообщение для Блондинка Посмотреть профиль Найти все сообщения от Блондинка
 
Регистрация: 24.02.2019
Сообщений: 806

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Gregorian & Julian converter via JDN (Julian Day Number)</title>
</head>
<body>
<small>
* The algorithm is valid for all (possibly proleptic) Gregorian dates ≥ 24.11.4714 BCE and Julian dates ≥ 01.01.4713 BCE.
<a href="https://en.wikipedia.org/wiki/Julian_day" target="_blank">References</a><br><br>
</small>
<form name="converter">
    <select name="wDay"></select>
    <label>Gn:</label>
    <select name="gDay"></select>
    <select name="gMonth"></select>
    <input type="number" name="gYear" min="-4713" style="width:8ch">
    <select name="gEra"></select>
    <input type="button" name="gCalc" value="to Jn">
    <label>Jn:</label>
    <select name="jDay"></select>
    <select name="jMonth"></select>
    <input type="number" name="jYear" min="-4712" style="width:8ch">
    <select name="jEra"></select>
    <input type="button" name="jCalc" value="to Gn">
    <br><br>
    <button type="button" class="express" value="g_n_o_w_ce">Now</button>
    <button type="button" class="express" value="g_15_10_1582_ce">Gn: 15.10.1582 CE</button>
    <button type="button" class="express" value="j_1_1_45_bce">Jn: 01.01.45 BCE</button>
    <button type="button" class="express" value="j_1_1_4713_bce">Jn: 01.01.4713 BCE</button>
    <button type="button" class="express" value="j_1_1_3268_ce">Jn: 01.01.3268 CE</button>
    <button type="button" class="express" value="j_1_1_1_ce">Jn: 01.01.1 CE</button>
    <!-- Express-button format C_D_M_Y_E: Calendar "j"/"g", Day, Month, Year, Era "ce"/"bce" -->
</form>
<script>
document.addEventListener('DOMContentLoaded', function () {
// https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
// The algorithms are valid for JDN >= 0, Gregorian >= 24.11.-4713, Julian >= 01.01.-4712
function gregorianToJdn(d, m, y) {
    return tr((1461 * (y + 4800 + tr((m - 14) / 12))) / 4) +
           tr((367 * (m - 2 - 12 * tr((m - 14) / 12))) / 12) -
           tr((3 * tr((y + 4900 + tr((m - 14) / 12)) / 100)) / 4) + d - 32075;
}
function julianToJdn(d, m, y) {
    return 367 * y - tr((7 * (y + 5001 + tr((m - 9) / 7))) / 4) + tr((275 * m) / 9) + d + 1729777;
}
function jdnToJulianOrGregorian(j, to) { // to: true (julian), false (gregorian)
    var f = (to) ? (j + 1401) : (j + 1363 + tr((tr((4 * j + 274277) / 146097) * 3) / 4));
    var e = 4 * f + 3, h = 5 * tr((e % 1461) / 4) + 2;
    var d = tr((h % 153) / 5) + 1, m = ((tr(h / 153) + 2) % 12) + 1, y = tr(e / 1461) - 4716 + tr((14 - m) / 12);
    return { d: d, m: m, y: y }; 
}
function jdnToWeekDay(j) { return (j % 7) + 1; }
function tr(n) { return n - n % 1; } // truncate (for integer division)
// -- wiki end --
var C = document.converter;
// options
var wdays = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
for (var o = '', i = 0; i < wdays.length; i++) o += '<option value="' + (i + 1) + '">' + wdays[i] + '</option>';
C.wDay.innerHTML = o;
for (var o = '', i = 1; i <= 31; i++) o += '<option value="' + i  + '">' + i + '</option>';
C.gDay.innerHTML = C.jDay.innerHTML = o;
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
for (var o = '', i = 0; i < months.length; i++) o += '<option value="' + (i + 1) + '">' + months[i] + '</option>';
C.gMonth.innerHTML = C.jMonth.innerHTML = o;
var eras = ['CE','BCE'], _eras = ['ce','bce'];
for (var o = '', i = 0; i < eras.length; i++) o += '<option value="' + _eras[i] + '">' + eras[i] + '</option>';
C.gEra.innerHTML = C.jEra.innerHTML = o;
// -- options end --
C.gCalc.onclick = function () {
    var gd = +C.gDay.value, gm = +C.gMonth.value, gy = historicalToAstronomical(+C.gYear.value, C.gEra.value);
    var jdn = gregorianToJdn(gd, gm, gy), wd = jdnToWeekDay(jdn), jd = jdnToJulianOrGregorian(jdn, true);
    var jy = astronomicalToHistorical(jd.y);
    C.wDay.value = wd, C.jDay.value = jd.d, C.jMonth.value = jd.m, C.jYear.value = jy.y, C.jEra.value = jy.e;
    onUpdateAll(jdn);
};
C.jCalc.onclick = function () {
    var jd = +C.jDay.value, jm = +C.jMonth.value, jy = historicalToAstronomical(+C.jYear.value, C.jEra.value);
    var jdn = julianToJdn(jd, jm, jy), wd = jdnToWeekDay(jdn), gd = jdnToJulianOrGregorian(jdn, false);
    var gy = astronomicalToHistorical(gd.y);
    C.wDay.value = wd, C.gDay.value = gd.d, C.gMonth.value = gd.m, C.gYear.value = gy.y, C.gEra.value = gy.e;
    onUpdateAll(jdn);
};
function initDate(c, d, m, y, e) {
    if (d + m + y == 'now') { var n = new Date(); d = n.getDate(), m = n.getMonth() + 1, y = n.getFullYear(); }
    C[c + 'Day'].value = d, C[c + 'Month'].value = m, C[c + 'Year'].value = y, C[c + 'Era'].value = e;
    C[c + 'Calc'].onclick();
}
function historicalToAstronomical(y, e) { // e: 'ce', 'bce'
    e = (y < 1) ? 'bce' : e;
    return ((e == 'bce') && y > 0) ? (1 - y) : y;
}
function astronomicalToHistorical(y) {
    var e = (y < 1) ? 'bce' : 'ce';
    return { y: (e == 'bce') ? -(y - 1) : y, e: e };
}
for (var ess = C.querySelectorAll('.express'), es, i = 0; i < ess.length; i++) {
     es = ess[i], es.onclick = Function.prototype.apply.bind(initDate, null, es.value.split('_'));
}
// Express-url format ...?C=D_M_Y_E
var m = location.search.match(/(j|g)=(\d+)_(\d+)_(\d+)_(ce|bce)/);
if (m) { initDate(m[1], m[2], m[3], m[4], m[5]); } else { initDate('g', 'n', 'o', 'w', 'ce'); }
function onUpdateAll(jdn) {
    console.log('JDN:', jdn);
    console.assert(jdn >= 2299161, 'Proleptic: Gn < 15.10.1582 CE');
    console.assert(jdn >= 1704987, 'Proleptic: Jn < 01.01.45 BCE');
    console.assert(jdn >= 0, 'Invalid: Gn < 24.11.4714 BCE, Jn < 01.01.4713 BCE');
}
});
</script>
</body>
</html>
Ответить с цитированием