2

Is there a Matlab code that transfer the date ( day,month,year) from gregorian

to Hijri (Islamic) calendar and also from hijri to gregorian calendar,

Let's assume that we want to change the gregorian date:

Friday, 18 / 11 / 2011

to the Hijri date which is Friday 22 / 12 / 1432

Thanks

Amro
  • 123,847
  • 25
  • 243
  • 454
user488182
  • 235
  • 6
  • 14
  • 1
    Please see the discussion here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/146181. It says "The Hirji calendar is different for different locations. There is therefore no generalized conversion algorithm possible, only historical data tables relating to some particular location." – petrichor Nov 17 '11 at 17:58
  • Thanks for your reply. Yes, it is known that Hijri calendar depends on moon and the converting between the two calendars have a maximum error of one day as seen from many converter programs in the internet. I want the formula of this conversion. Also, I found a java script in this link: http://stackoverflow.com/questions/5177598/how-to-convert-gregorian-date-to-hijri-date but I don't know ho translate it to Matlab. regards – user488182 Nov 17 '11 at 18:36
  • I also googled but couldn't find a ready-to-use code for that. I guess you will be the first :) – petrichor Nov 17 '11 at 19:04
  • I think the code is already written in the above link but it is based on java, I will try to translate it to Matlab. – user488182 Nov 17 '11 at 19:15

1 Answers1

2

If you are on Windows, you could use the .NET Framework from inside MATLAB.

Here is a function to convert Gregorian dates to Hijri (based on an article on CodeProject):

function out = GregToHijri(str, frmtIn, frmtOut)
    % English (US) and Arabic (Saudi Arabia) cultures
    enCult = System.Globalization.CultureInfo('en-US',false);
    enCult.DateTimeFormat.Calendar = System.Globalization.GregorianCalendar();
    arCult = System.Globalization.CultureInfo('ar-SA',false);
    arCult.DateTimeFormat.Calendar = System.Globalization.HijriCalendar();

    % parse using supplied input format
    dt = System.DateTime.ParseExact(str, frmtIn, enCult.DateTimeFormat);

    % convert datetime as formatted string
    out = char( dt.ToString(frmtOut, arCult.DateTimeFormat) );
end

Tested on your input:

>> GregToHijri('Friday, 18/11/2011', 'dddd, dd/MM/yyyy', 'dd/MM/yyyy')
ans =
22/12/1432
Amro
  • 123,847
  • 25
  • 243
  • 454