The Hijri year is a pure lunar year of an average length of 354.36666 days
having 12 months of average length of 29.53058 days.
  The Epoch of the Hijri calendar is 1 Moharram 1 which is equivalent to
July 16, 622 AC Julian;Moharram's new moon was born on July 14th. 
 
  The Julian/Gregorian year is a solar year is 365.25 days long, with 12 months 
of 30.6 days average length.
   
 I refer to Gregorian after Oct 5,1582(when the pope added 10 days 
to the Julian calendar), so OCT 15,1582 Gregorian is equal to OCT 5, 1582 
Julian! 
so I'll use the word Miladi for both Gregorian and Julian.. 


Steps: 
------ 
A-Converting from Hijri to Miladi dates: 
_________________________________________ 
  
  Miladi date is taken as d, m , y(day, month,year) 
   1- Calculate The days elapsed from the Hijri Epoch to the current 
      Hijri date(in question).( 
 
 1- For after Hijra use: 
    X = [(y - 1)*354.366] + [(m -1)*29.53] + d + 196 + C 
    For Before Hijra use: 
    X = [(y - 1)*354.366] + [(13-m)*29.53] -(d-1)-196   C=0 here. 
     round X to the highest integer, in days.   
     where: 
    X = Elapsed days from Hijri epoch to current Hijri date in days. 
    C = the accumulating difference between Julian year and 
        the gregorian. it accumulates 1 day for about 128 years. 
        for our times it is equal to 13 days. 
        before OCT 15, 1582(Oct 5 in Julian), take C=0 
        on     // take C=10, and  so on. 
 
        196 days = the difference between 1 January and 16 July, 622 
 
  2-   Miladi Year, 
       After Hijra use: 
       Y = 622 + Integer part(X/365.25) 
       Before Hijra, use: 
       Y = 622 - Integer part(X/365.25) 

  3-   Miladi Month,M= Integral part(12*[Decimal part(X)]) + 1 
  4-   Miladi Day,  D = 30.6*Fractional part(12*[Decimal part(X)]) 
                    rounded up to the highest integer. 
 
   where the Average solar Month= 30.6 days. 
 
EXAMPLE after hijra: 
-------------------- 
  Hijri date 1 Safar, 1421(current month) 
    d=1, m=2,  y=1421 
 
    X = [(1421-1)*354.367]+[1*29.53058]+1+196+13 
      = 503440.67058 days 
    X/365.25 = 1378.345436222 
    Miladi Year , 
    Y = 622 +integral part(503439/365.25)= 622+1378= 2000 
    Miladi Month, 
    M = Integral part(0.345436222 * 12) +1 = Int part(4.145234664)+1 
      = 4+1=5  ==>  Month= May 
    D = 30.6*(.145234664) = 4.444, round it up to 5 
    D = 5 
 

EXAMPLE, before Hijra: 
--------------------- 
Hijri date 1 Moharam 54 B.H.(Beginning of Aam Al-Feel) 
d=1 ,  m= 1 , y=54 ( actually -54) (there is no y=0) 

1-  X =[54-1]*354.366 + [29.53058*(13-1)] -(1-1)-196=18939.811 
    round it to an integer, X=18940 
   divide by 365.25 and subtract from 622 
   you get 570.14510609 
then: 
2- Miladi year = 570 AC 

3- take .14510609 multiply it by 12 , you get 1.74127308 
   take the integral part and add +1, you get: 
    Miladi Month = 2  February 
4- take .74127308 and multiply it by 30.6, you get 22.68 
   if you round it you get: 
   Miladi day=23 
   so it is 23 February 570 Ac 
   Comparing with Astro data: 
   New moon is at 1:41 AM (Mecca time) 21 Feb 570 
   at sunset it will be around 16 to 17 hours(critical) 

B- CONVERTING FROM MILADI TO HIJRI: 
__________________________________ 
  d= day, m=month, y= Year     Miladi date 
1- Elapsed days from Hijri epoch 

  After Hijra use: 
  X = [y - 622]*365.25 + D - 196 - C 
  Before Hijra, use: 
  X = [622 - y]*365.25 - D + 196  C=0 here. 

again take C as above. 
2- After Hijra, use:  Y =[x/354.366] + 1 
   Before Hijra, use: Y =[x/354.366] 
3- Hijri Year y = Integar part(Y) 
4- Z = [ Y - integer part(Y)]*12 
 Hijri Month m = integer part(Z) +1 
5- Hijri day d = integer part( fraction part(Z))*29.53 
------------------------------------------------------- 
Example, Miladi to Hijri: 
_______________________ 

A- Before Hijra: 
----------------- 
   ex:  d=23 m=2 y=570  (D = 31+23=54  jan till feb 23) 
 1- calculate X and divide by 354.366(354.367 if you want) 
    you get 54.00051359 
    then Hijri year=54 (Before hijra for sure) 
 2- .00051359 x 12 = 0.006163092, the integer part =0 
    then: Hijri Month = 0 +1 =1  Moharram 

 3- .006163092 *29.53058 = 0.181999681, obviously 

    Hijri day =1 
   then it is  1 Moharram 54 B.H. 
B- After Hijra: 
---------------- 
  5 May, 2000 
  d=5, m=5, y=2000 

1- X=503231.5 
   divide by 354.367 you get 1420.122359 
   then Hijri year=1420+1=1421 
2- .0280856 x 12 = 1.0280856 
   then Hijri Month = 1 + 1 =2  Safar 
3- .0280856 x 29.53058 = 0.829384057 

   round it , you get 
   Higri day = 1 
   1 Safar 1421 AH