3 Replies Latest reply on Jan 28, 2013 5:18 AM by BarlaeDC

    gregorian to hijri and viceversa javascript

    KKBDEL

      I have a Javascript for a Hijri / Gregorian calendar.

      I need to get it into Adobe Acrobat PDF.

      The business requirements for this are easy. Just take the script i give you and get it to work in Adobe Acrobat PDF.

      The technical side is tricky. I haven't been able to make it work. When I do it, the conversion works 1 time when you first open the document. then, it only works one way after that. (from gregorian to hijri, never the other way around.)


       

      1. This MUST be done in Acrobat XI, Not LiveCycle.

      2. I'm using Adobe Acrobat XI Pro. I can't use any other version right now. So this must be able to work on XI and then people need to be able to open and execute this in Reader.


      Here is the code I have written from a HTM file..

      ****************************************************************************************** **************************************

      <SCRIPT LANGUAGE="JavaScript">

        function setDateToToday()

      {

          var today = new Date();

          var y = today.getYear();

       

          var y = today.getYear();

          if (y < 1000) {

              y += 1900;

          }

        

          document.myform.CYear.value = y;

          document.myform.CMonth.selectedIndex = today.getMonth();

          document.myform.CDay.value = today.getDate();

      // Travis added the following method call to fix the bug where the Hijri date was not set to today by default   

          chrToIsl(0);

      }

       

      //  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

       

      function gregorian_to_jd(year, month, day)

      {

         

          var GREGORIAN_EPOCH = 1721425.5;

          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);

      }

       

       

      function mod(a, b)

      {

          return a - (b * Math.floor(a / b));

      }

       

       

      function jd_to_gregorian(jd) {

          var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad,

              yindex, dyindex, year, yearday, leapadj;

       

       

          var GREGORIAN_EPOCH = 1721425.5;

          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;

       

       

                document.myform.CDay.value = day;

                document.myform.CMonth.value = month;

                document.myform.CYear.value = year;

      }

          

      function intPart(floatNum){

      if (floatNum< -0.0000001){

                 return Math.ceil(floatNum-0.0000001)

                }

      return Math.floor(floatNum+0.0000001)

      }

         

      function weekDay(wdn){

      // Stubbed this out because we have no interest in showing the days of the week.

        return ""  }

       

      function chrToIsl(NumberofDays) {

                //alert("Month:" + document.myform.CMonth.value)

                d=parseInt(document.myform.CDay.value)

                m=parseInt(document.myform.CMonth.value)

                y=parseInt(document.myform.CYear.value)

                                                        if ((y>1582)||((y==1582)&&(m>10))||((y==1582)&&(m==10)&&(d>14)))

                                                                  {

                                                                  jd=intPart((1461*(y+4800+intPart((m-14)/12)))/4)+intPart((3 67*(m-2-12*(intPart((m-14)/12))))/12)-

                intPart( (3* (intPart(  (y+4900+    intPart( (m-14)/12)     )/100)    )   ) /4)+d-32075

                                                                  }

                                                                  else

                                                                  {

                                                                  jd = 367*y-intPart((7*(y+5001+intPart((m-9)/7)))/4)+intPart((275*m)/9)+d+1729777

                                                                  }

                                                        jd = jd + NumberofDays

                                                        //Stubbed out the weekday/epoch names below to remove them from the HTML page.  jd = epoch time wd = weekday

                                                        //document.myform.JD.value=jd

                                                        //document.myform.wd.value=weekDay(jd%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/5 670))*(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

         

                jd_to_gregorian(jd)

                document.myform.HDay.value=d

                document.myform.HMonth.value=m

                document.myform.HYear.value=y

      }

       

      function islToChr( NumberofDays) {

                d=parseInt(document.myform.HDay.value)

                m=parseInt(document.myform.HMonth.value)

                y=parseInt(document.myform.HYear.value)

                jd=intPart((11*y+3)/30)+354*y+30*m-intPart((m-1)/2)+d+1948440-385

                jd = jd + NumberofDays

                //Stubbed out the weekday/epoch names below to remove them from the HTML page.  jd = epoch time wd = weekday

                //document.myform.JD.value=jd

                //document.myform.wd.value=weekDay(jd%7)

                                                        if (jd> 2299160 )

                                                                  {

                                                                   l=jd+68569

                                                                   n=intPart((4*l)/146097)

                                                                  l=l-intPart((146097*n+3)/4)

                                                                   i=intPart((4000*(l+1))/1461001)

                                                                  l=l-intPart((1461*i)/4)+31

                                                                   j=intPart((80*l)/2447)

                                                                  d=l-intPart((2447*j)/80)

                                                                  l=intPart(j/11)

                                                                  m=j+2-12*l

                                                                  y=100*(n-49)+i+l

                                                                  }

                                                        else

                                                                  {

                                                                   j=jd+1402

                                                                   k=intPart((j-1)/1461)

                                                                   l=j-1461*k

                                                                   n=intPart((l-1)/365)-intPart(l/1461)

                                                                   i=l-365*n+30

                                                                  j=intPart((80*i)/2447)

                                                                  d=i-intPart((2447*j)/80)

                                                                  i=intPart(j/11)

                                                                  m=j+2-12*i

                                                                  y=4*k+n+i-4716

                                                                  }

       

                document.myform.CDay.value=d

                document.myform.CMonth.value=m

                document.myform.CYear.value=y

      }

      </SCRIPT>

          <FORM NAME="myform">

      <strong>gregorian</strong>

       

       

        <select size="1" name="CMonth" onChange="chrToIsl(0);">

          <option selected value="1">1</option>

          <option value="2">2</option>

          <option value="3">3</option>

          <option value="4">4</option>

          <option value="5">5</option>

          <option value="6">6</option>

          <option value="7">7</option>

          <option value="8">8</option>

          <option value="9">9</option>

          <option value="10">10</option>

          <option value="11">11</option>

          <option value="12">12</option>

        </select>

       

      <select size="1" name="CDay"  onChange="chrToIsl(0);">

          <option selected value="1">1</option>

          <option value="2">2</option>

          <option value="3">3</option>

          <option value="4">4</option>

          <option value="5">5</option>

          <option value="6">6</option>

          <option value="7">7</option>

          <option value="8">8</option>

          <option value="9">9</option>

          <option value="10">10</option>

          <option value="11">11</option>

          <option value="12">12</option>

          <option value="13">13</option>

          <option value="14">14</option>

          <option value="15">15</option>

          <option value="16">16</option>

          <option value="17">17</option>

          <option value="18">18</option>

          <option value="19">19</option>

          <option value="20">20</option>

          <option value="21">21</option>

          <option value="22">22</option>

          <option value="23">23</option>

          <option value="24">24</option>

          <option value="25">25</option>

          <option value="26">26</option>

          <option value="27">27</option>

          <option value="28">28</option>

          <option value="29">29</option>

          <option value="30">30</option>

          <option value="31">31</option>

        </select>

       

      <INPUT TYPE="text" NAME="CYear" VALUE="" SIZE="4" onChange="chrToIsl(0);" onKeyUp="chrToIsl(0);" >

        </p>

      <strong>hijri</strong>

      <select size="1" name="HMonth" onChange="islToChr(0);">

          <option selected value="1">1</option>

          <option value="2">2</option>

          <option value="3">3</option>

          <option value="4">4</option>

          <option value="5">5</option>

          <option value="6">6</option>

          <option value="7">7</option>

          <option value="8">8</option>

          <option value="9">9</option>

          <option value="10">10</option>

          <option value="11">11</option>

          <option value="12">12</option>

        </select>

      <select size="1" name="HDay" onChange="islToChr(0);">

          <option selected value="1">1</option>

          <option value="2">2</option>

          <option value="3">3</option>

          <option value="4">4</option>

          <option value="5">5</option>

          <option value="6">6</option>

          <option value="7">7</option>

          <option value="8">8</option>

          <option value="9">9</option>

          <option value="10">10</option>

          <option value="11">11</option>

          <option value="12">12</option>

          <option value="13">13</option>

          <option value="14">14</option>

          <option value="15">15</option>

          <option value="16">16</option>

          <option value="17">17</option>

          <option value="18">18</option>

          <option value="19">19</option>

          <option value="20">20</option>

          <option value="21">21</option>

          <option value="22">22</option>

          <option value="23">23</option>

          <option value="24">24</option>

          <option value="25">25</option>

          <option value="26">26</option>

          <option value="27">27</option>

          <option value="28">28</option>

          <option value="29">29</option>

          <option value="30">30</option>

        </select>

       

      <INPUT TYPE="text" NAME="HYear" VALUE="1430" SIZE="4" onChange="islToChr(0);" onKeyUp="islToChr(0);">

       

      <script>

      setDateToToday();

      </script>

      ****************************************************************************************** ****************************

       

       

      I just need the functionality if it can be achieved with a better script.

        • 1. Re: gregorian to hijri and viceversa javascript
          BarlaeDC Level 4

          Hi,

           

          Please find a sample here --https://workspaces.acrobat.com/?d=6xKYZX0bCChJ9obJ0cUXWA

           

          This is a very simple sample that uses your code in an Acrobat PDF, created using Acrobat XI and tested in Reader XI.

           

          The main functions ( takaen from you post above) are placed in a document level JavaScript, and the functions necessary are called by the repective buttons on the form.

           

          Hope this helps

           

          Malcolm

          • 2. Re: gregorian to hijri and viceversa javascript
            KKBDEL Level 1

            Thanks a lot Barlae..

             

            I appreciate your efforts and really want to thank you ..

            I have found Two issues. One, it doesn't convert to Hijri date, only Julian date for Hijri (doesn't show the Hijri date but does show two Gregorian dates).


            Second, I want to be able to have the dates default (if blank) to today's date. Then when any field is changed in one field (Hijri or Gregorian), it automatically changes in the other. if possibiliy with no buttons.,something like as soon as a value changes in any of the three fields ( day,month and year) the calculations run automatically ,rather than pressing any button.

             

            Hope to have your expert help on this.

             

            Regards

            Kapil

            • 3. Re: gregorian to hijri and viceversa javascript
              BarlaeDC Level 4

              Hi,

               

              Issue 1 - it doesn't convert to Hijri date, only Julian date for Hijri (doesn't show the Hijri date but does show two Gregorian dates).

               

              I am sorry I don't actually know how to convert between the dates, I only used the code that you provided in your initial post and called that from the Acrobat PDF form,

               

              The sample I provided is how to call the code above using Acrobat.

               

              Issue 2 -  I want to be able to have the dates default (if blank) to today's date

               

              That is easy enough to do in that you just use JavaScript to get the current date and time using the Date object.

               

              And it can convert dynamically as you could just attach the code to the change or exit events of the objects that you are using to select the date and run the code.

               

              I kept my sample simple and used buttons so it is easy for everyone to see how the code is called as you can look at the preferences of each button to see the JavaScript that is used.

               

              Regards

               

              Malcolm