How to make Gregorian and Islamic calendar in one (javascript)

Santhoshkumar Sivaji picture Santhoshkumar Sivaji · Oct 19, 2013 · Viewed 9k times · Source

I am trying to make calendar which contain both Gregorian and Islamic date in one,I just rough coded Gregorian calendar like bellow but i don't have idea that how to include Islamic date with this,could any one please give me idea that how to make this

I want to make calendar like this http://www.islamicfinder.org/Hcal/index.php

my code is :

function Calendar()
{
    var now=new Date();
    //---------------current date and time------------------
    var day=now.getDate();
    var month=now.getMonth()+1;
    var year=now.getFullYear();
    //-----------------------------------------------------

    var FirstDay=new Date(now.getFullYear(),now.getMonth(),1);
    var dayOne=FirstDay.getDay();
    var LastDay=new Date(now.getFullYear(),now.getMonth()+1,0);
    var dayLast=LastDay.getDay();
    var noDays=getMonthDays(year,month);

    var abs="<table><tr><td>sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Th</td><td>Fri</td><td>sat</td></tr><tr>";
    var iterator=0;;
    while(dayOne>0)
    {
        abs +="<td></td>"
        iterator++;
        dayOne--;
    }
    var i=1;
    alert(month);
    alert(year)
    while(i<=noDays)
    {
        if(iterator>6)
        {
            iterator=0;
            abs +="</tr><tr>"
        }
        abs +="<td>"+i+"</td>";
        iterator++
        i++;
    }
    var nameOfMonth=monthName(month);
    document.getElementById("head").innerHTML="<h1>"+nameOfMonth+"</h1>";
    document.getElementById("tble").innerHTML=abs;
}
function getMonthDays(iyear,imonth)
{
    if (((iyear % 4)==0) && ((iyear % 100)!=0) || ((iyear % 400)==0))
    {
        var days=new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
        return days[imonth];
    }
    else
    {
        var days=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
        return days[imonth];
    }

}
function monthName(m)
{
    var months=new Array("nill","January","February","March","April","May","June","July","August","September","October","November","December");
    //alert(months[m]);
    return months[m];
}

Answer

Santhoshkumar Sivaji picture Santhoshkumar Sivaji · Oct 24, 2013

I finally coded Calendar with the help of Gregorian to Islamic Date converter my final code is:

    function Calendar()
{

    var now=new Date();
    //---------------current date and time------------------
    var day=now.getDate();
    var month=now.getMonth()+1;
    var year=now.getFullYear();
    //-----------------------------------------------------

    var FirstDay=new Date(now.getFullYear(),now.getMonth()+1,now.getDate());
    var dayOne=FirstDay.getDay();
    var LastDay=new Date(now.getFullYear(),now.getMonth()+1,0);
    var dayLast=LastDay.getDay();
    var noDays=getMonthDays(year,month);

    var abs="<table class='cal' width='100%' cellspacing='0' cellpadding='3' border='0'><tr><td>sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Th</td><td>Fri</td><td>sat</td></tr><tr>";
    var iterator=0;;
    while(dayOne>0)
    {
        abs +="<td></td>"
        iterator++;
        dayOne--;
    }
    var i=1;

    while(i<=noDays)
    {
        if(iterator>6)
        {
            iterator=0;
            abs +="</tr><tr>"
        }
        abs +="<td>"+i+"<div class='test'> "+GregToIsl(month,i,year)+"</div>"+"</td>";
        iterator++
        i++;
    }
    var nameOfMonth=monthName(month);
    document.getElementById("head").innerHTML="<h1>"+nameOfMonth+"</h1>";
    document.getElementById("tble").innerHTML=abs;
}
function getMonthDays(iyear,imonth)
{
    if (((iyear % 4)==0) && ((iyear % 100)!=0) || ((iyear % 400)==0))
    {
        var days=new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
        return days[imonth];
    }
    else
    {
        var days=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
        return days[imonth];
    }

}
function monthName(m)
{
    var months=new Array("nill","January","February","March","April","May","June","July","August","September","October","November","December");
    //alert(months[m]);
    return months[m];
}




function test(val)
{
    return val+1;
}



function intPart(floatNum){
    if (floatNum< -0.0000001){
    return Math.ceil(floatNum-0.0000001)
    }
return Math.floor(floatNum+0.0000001)
}
function GregToIsl(mnth,day,year)
    {

d=parseInt(day)
m=parseInt(mnth)
y=parseInt(year)

delta=0
if ((y>1582)||((y==1582)&&(m>10))||((y==1582)&&(m==10)&&(d>14)))
      {
          //added +delta=1 on jd to comply isna rulling 2007
          jd=intPart((1461*(y+4800+intPart((m-14)/12)))/4)+intPart((367*(m-2-12*(intPart((m-14)/12))))/12)-
              intPart( (3* (intPart(  (y+4900+    intPart( (m-14)/12)     )/100)    )   ) /4)+d-32075+delta
          }
else
      {
          //added +1 on jd to comply isna rulling
          jd = 367*y-intPart((7*(y+5001+intPart((m-9)/7)))/4)+intPart((275*m)/9)+d+1729777+delta
          }
//arg.JD.value=jd
//added -1 on jd1 to comply isna rulling
jd1=jd-delta
//arg.wd.value=weekDay(jd1%7)
l=jd-1948440+10632
n=intPart((l-1)/10631)
l=l-10631*n+354
j=(intPart((10985-l)/5316))*(intPart((50*l)/17719))+(intPart(l/5670))*(intPart((43*l)/15238))
l=l-(intPart((30-j)/15))*(intPart((17719*j)/50))-(intPart(j/16))*(intPart((15238*j)/43))+29
m=intPart((24*l)/709)
d=l-intPart((709*m)/24)
y=30*n+j-30


return d+"-"+m;


}

this may be not optimized method but this is working