将公历日期转换为Hijri日期

时间:2011-03-03 06:51:02

标签: javascript date gregorian-calendar date-conversion hijri

如何使用JavaScript将格里高利日期转换为伊斯兰日历日期?

3 个答案:

答案 0 :(得分:18)

function gmod(n,m){
return ((n%m)+m)%m;
}

function kuwaiticalendar(adjust){
var today = new Date();
if(adjust) {
    adjustmili = 1000*60*60*24*adjust; 
    todaymili = today.getTime()+adjustmili;
    today = new Date(todaymili);
}
day = today.getDate();
month = today.getMonth();
year = today.getFullYear();
m = month+1;
y = year;
if(m<3) {
    y -= 1;
    m += 12;
}

a = Math.floor(y/100.);
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;
    }
}

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.);
}
bb = jd+b+1524;
cc = Math.floor((bb-122.1)/365.25);
dd = Math.floor(365.25*cc);
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;


wd = gmod(jd+1,7)+1;

iyear = 10631./30.;
epochastro = 1948084;
epochcivil = 1948085;

shift1 = 8.01/60.;

z = jd-epochastro;
cyc = Math.floor(z/10631.);
z = z-10631*cyc;
j = Math.floor((z-shift1)/iyear);
iy = 30*cyc+j;
z = z-Math.floor(j*iyear+shift1);
im = Math.floor((z+28.5001)/29.5);
if(im==13) im = 12;
id = z-Math.floor(29.5001*im-29);

var myRes = new Array(8);

myRes[0] = day; //calculated day (CE)
myRes[1] = month-1; //calculated month (CE)
myRes[2] = year; //calculated year (CE)
myRes[3] = jd-1; //julian day number
myRes[4] = wd-1; //weekday number
myRes[5] = id; //islamic date
myRes[6] = im-1; //islamic month
myRes[7] = iy; //islamic year

return myRes;
}
function writeIslamicDate(adjustment) {
var wdNames = new Array("Ahad","Ithnin","Thulatha","Arbaa","Khams","Jumuah","Sabt");
var iMonthNames = new Array("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(adjustment);
var outputIslamicDate = wdNames[iDate[4]] + ", " 
+ iDate[5] + " " + iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}

这会将当前计算机日期转换为hijri。通过一些修改,您可以实现此片段将任何日期更改为伊斯兰

document.write(writeIslamicDate(1));

取自This site

答案 1 :(得分:3)

如果您只需要从格里高利日期(米拉迪日期)转换的Hijri日期年份,您可以在javascript中简单地写出这个等式:

var GregorianYear = (new Date()).getFullYear();
var HijriYear = Math.round((GregorianYear - 622) * (33 / 32));

您可以在母版页面的页脚中使用这个简单的公式 كلالحقوقمحفوطةل...©

<script type="text/javascript">document.write((new Date()).getFullYear())</script> م - <script type="text/javascript">var y = (new Date()).getFullYear();var h = Math.round((y - 622) * (33 / 32));document.write(h)</script> هـ
你会得到: كلالحقوقمحفوطةل...©2014م - 1435ه

您也可以使用嵌入在asp页面中的C#:

<%= DateTime.Now.Year %> - <%=  Math.Round((DateTime.Now.Year - 622) * 1.03125) %>

将返回:2014 - 1436

答案 2 :(得分:1)

结帐我的图书馆hijrah-date这是Hijrah日历系统中的Javascript日期。

它还支持Hijrah到Gregorian和Gregorian到Hijrah的转换。除了日期格式。