11

Now i am working with Hijri dates and trying to convert them to Gregorian dates using the following code :

string HijriDate;
string[] allFormats ={"yyyy/MM/dd","yyyy/M/d",
    "dd/MM/yyyy","d/M/yyyy",
    "dd/M/yyyy","d/MM/yyyy","yyyy-MM-dd",
    "yyyy-M-d","dd-MM-yyyy","d-M-yyyy",
    "dd-M-yyyy","d-MM-yyyy","yyyy MM dd",
    "yyyy M d","dd MM yyyy","d M yyyy",
    "dd M yyyy","d MM yyyy","MM/dd/yyyy"};
CultureInfo enCul = new CultureInfo("en-US");
CultureInfo arCul = new CultureInfo("ar-SA");
arCul.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar(); 
DateTime tempDate = DateTime.ParseExact(HijriDate, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString("MM/dd/yyyy");

this code is working fine with all dates except the date that has 30th day in month like the following :

30/10/1433, 30/12/1432 or 30/05/1433 etc. so how to handle and convert that date with its corresponding Gregorian

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
special life
  • 225
  • 1
  • 7
  • 20

3 Answers3

10

here is the code it is working well now on this code I'm returning the date from the function as string not as datetime, but you can simply using return datetime type instead on string

public string ConvertDateCalendar(DateTime DateConv, string Calendar, string DateLangCulture)
{
    System.Globalization.DateTimeFormatInfo DTFormat;
    DateLangCulture = DateLangCulture.ToLower();
    /// We can't have the hijri date writen in English. We will get a runtime error - LAITH - 11/13/2005 1:01:45 PM -

    if (Calendar == "Hijri" && DateLangCulture.StartsWith("en-"))
    {
        DateLangCulture = "ar-sa";
    }

    /// Set the date time format to the given culture - LAITH - 11/13/2005 1:04:22 PM -
    DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;

    /// Set the calendar property of the date time format to the given calendar - LAITH - 11/13/2005 1:04:52 PM -
    switch (Calendar)
    {
        case "Hijri":
            DTFormat.Calendar = new System.Globalization.HijriCalendar();
            break;

        case "Gregorian":
            DTFormat.Calendar = new System.Globalization.GregorianCalendar();
            break;

        default:
            return "";
    }

    /// We format the date structure to whatever we want - LAITH - 11/13/2005 1:05:39 PM -
    DTFormat.ShortDatePattern = "dd/MM/yyyy";
    return (DateConv.Date.ToString("f", DTFormat));
}

To call this Method here are an example

ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Gregorian", "en-US");

To call the Hijri

ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Hijri", "en-US");
Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
Ahmad Hindash
  • 1,519
  • 15
  • 16
  • I am getting: System.ArgumentOutOfRangeException: Specified time is not supported in this calendar. It should be between 07/18/0622 00:00:00 (Gregorian date) and 12/31/9999 23:59:59 (Gregorian date), inclusive. When I try the above code. – Ahmad Hijazi Sep 17 '19 at 12:43
0

Max Value of a days in a month can be calculated by DateTime.DaysInMonth(year, month)

and use it this way

int result = DateTime.DaysInMonth(2012, 2); // returns 29 being a leap year

but

int result = DateTime.DaysInMonth(2011, 2) // returns 28 being a non-leap year
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • yeah my problem is when you deal with hijri dates it is not constant like Gregorian months because it is depends on Crescent so maybe 5th month is 30 days this year but next year maybe 29 days only so the problem usually will be with 30th day for any month and this is my problem now :S – special life Jun 26 '12 at 05:01
  • In that case you have to create your own function like `DateTime.DaysInMonth(year, month)` where you would send year and month and it will return days in that month. – Nikhil Agrawal Jun 26 '12 at 05:27
-1

10th and 12nd months in Hirji don't have 30th day, so that date is invalid.

nothrow
  • 15,882
  • 9
  • 57
  • 104