Here the code...
<script>
var PERSIAN_EPOCH = 1948320.5;
datos = window.AppInventor.getWebViewString(); // Entrada de datos App Inventor
partes = datos.split(",");
var years = partes[0];
var months = partes[1];
var days = partes[2];
/* MOD -- Modulus function which works for non-integers. (from file calendar.js original) */
function mod(a, b)
{
return a - (b * Math.floor(a / b));
}
// PERSIAN_TO_JD -- Start data here
function persian_to_jd(year, month, day)
{
var epbase, epyear;
epbase = year - ((year >= 0) ? 474 : 473);
epyear = 474 + mod(epbase, 2820);
var salida = Number(day) + Number(((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6))) +
Number(Math.floor(((epyear * 682) - 110) / 2816)) +
Number((epyear - 1) * 365) +
Number(Math.floor(epbase / 2820) * 1029983) +
Number(PERSIAN_EPOCH -1);
// window.AppInventor.setWebViewString(salida); // Juliano. Respuesta a CadenaDeWebView
return salida
}
// 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)));
}
// GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date
var GREGORIAN_EPOCH = 1721425.5;
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_GREGORIAN -- Calculate Gregorian calendar date from Julian day
function jd_to_gregorian(jd) {
var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad,
yindex, dyindex, year, yearday, leapadj;
wjd = Math.floor(jd - 0.5) + 0.5;
depoch = wjd - GREGORIAN_EPOCH;
quadricent = Math.floor(depoch / 146097);
dqc = mod(depoch, 146097);
cent = Math.floor(dqc / 36524);
dcent = mod(dqc, 36524);
quad = Math.floor(dcent / 1461);
dquad = mod(dcent, 1461);
yindex = Math.floor(dquad / 365);
year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
if (!((cent == 4) || (yindex == 4))) {
year++;
}
yearday = wjd - gregorian_to_jd(year, 1, 1);
leapadj = ((wjd < gregorian_to_jd(year, 3, 1)) ? 0
:
(leap_gregorian(year) ? 1 : 2)
);
month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
day = (wjd - gregorian_to_jd(year, month, 1)) + 1;
// Salida de calendario Gregoriano.
window.AppInventor.setWebViewString(year + "," + month + "," + day); // Respuesta a CadenaDeWebView
return new Array(year, month, day);
}
var juliano = persian_to_jd(years,months,days);
var gregoriano = jd_to_gregorian(juliano);
</script>
Look at the Persian, Julian and Gregorian conversion algorithm.
As well as App Inventor data input and output.
calendario_persa.aia (3.6 KB)