// Returns mathematic mod, not javascript mod
// e.g. gmod(-3, 2) returns 1, whereas -3%2 returns -1
function gmod(n, m){
return ((n % m) + m) % m;
}
/* @param {Date} date - optional, default is today
** @param {number} adjust - optiona, days to adjust date by
*/
function kuwaiticalendar(date, adjust) {
var today = date? new Date(+date) : new Date();
if (adjust) {
today.setDate(today.getDate() + +adjust);
}
var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();
var m = month + 1;
var y = year;
if (m < 3) {
y -= 1;
m += 12;
}
var a = Math.floor(y/100);
var b = 2 - a + Math.floor(a/4);
if (y < 1583) b = 0;
if (y == 1582) {
if (m > 10) b = -10;
if (m == 10) {
b = 0;
if (day > 4) b = -10;
}
}
var jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;
b = 0;
if(jd > 2299160){
a = Math.floor((jd - 1867216.25)/36524.25);
b = 1+a-Math.floor(a/4);
}
var bb = jd+b+1524;
var cc = Math.floor((bb - 122.1)/365.25);
var dd = Math.floor(365.25 * cc);
var ee = Math.floor((bb - dd)/30.6001);
day = (bb - dd) - Math.floor(30.6001 * ee);
month = ee - 1;
if (ee > 13) {
cc += 1;
month = ee - 13;
}
year = cc - 4716;
var wd = gmod(jd + 1, 7) + 1;
var iyear = 10631./30.;
var epochastro = 1948084;
var epochcivil = 1948085;
var shift1 = 8.01/60.;
var z = jd-epochastro;
var cyc = Math.floor(z/10631.);
z = z - 10631 * cyc;
var j = Math.floor((z - shift1)/iyear);
var iy = 30 * cyc + j;
z = z - Math.floor(j * iyear + shift1);
var im = Math.floor((z + 28.5001)/29.5);
if (im == 13) im = 12;
var id = z-Math.floor(29.5001*im-29);
return [
day, //calculated day (CE)
month - 1, //calculated month (CE)
year, //calculated year (CE)
jd - 1, //julian day number
wd - 1, //weekday number
id, //islamic date
im - 1, //islamic month
iy //islamic year
];
}
function writeIslamicDate(date, adjustment) {
var wdNames = ["Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams", "Jumuah", "Sabt"];
var iMonthNames = ["Muharram", "Safar", "Rabi'ul Awwal", "Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira",
"Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja"];
var iDate = kuwaiticalendar(date, adjustment);
var outputIslamicDate = wdNames[iDate[4]] + ", " + iDate[5] + " " +
iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
// No date or adjust
console.log(writeIslamicDate());
// No date, adjust to tomorrow
console.log(writeIslamicDate(undefined, 1));
// Date for 1 Jan 2017
console.log(writeIslamicDate(new Date(2017,0,1)));
// Date for 1 Jan 2017, adjust by -1 day
console.log(writeIslamicDate(new Date(2017,0,1), -1));
感謝。那麼是否應該下載moment.js對不對?我試着用document.write(moment('2014-11-28 16:40:00','YYYY-MD HH:mm:ss')。endOf('iMonth')。format('iYYYY/iM/iH HH :MM:SS'));並沒有得到什麼 –
是的應該,你可以使用鮑爾https://github.com/xsoh/moment-hijri –
什麼可能是2014-11-28 16:40:00回覆日期?我得到了i2014/i11/i28 16:40:00? –