Hijri Date To gregorian using DateTime.Parse RRS feed

  • Question

  • Dear,

    I have the following code snitp to convert from 30/08/1434 to Gregorian , It fails the conversion cause the format is not correct , if you change the date by one day plus or minus with the same format the code works , is this a bug or am I doing something wrong .

    Thanks for your help.

     class Program
            static void Main(string[] args)
                CultureInfo arSA = new CultureInfo("ar-SA");
                string dateString;
                DateTime dateValue;
                // Parse date with no style flags.
                dateString = "30/08/1434";
                if (DateTime.TryParseExact(dateString, "dd/MM/yyyy", arSA,
                                           DateTimeStyles.None, out dateValue))
                    Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
                    Console.WriteLine("'{0}' is not in an acceptable format.", dateString);


    Thursday, July 11, 2013 4:03 PM


  • Hi,

    I am not familiar with Hijri calendar. However, I tried the following code:

    CultureInfo arSA = new CultureInfo("ar-SA");
    arSA.DateTimeFormat.Calendar = new HijriCalendar();
    arSA.DateTimeFormat.Calendar.ToDateTime(1434, 8, 30, 0, 0, 0, 0);

    Then the following error is raised (for more information about this error message, please see http://msdn.microsoft.com/en-us/library/System.Globalization.HijriCalendar.aspx).

    "Day must be between 1 and 29 for month 8."

    In fact, the following code works.

    CultureInfo arSA = new CultureInfo("ar-SA");
    arSA.DateTimeFormat.Calendar = new HijriCalendar();
    var dateValue = DateTime.ParseExact("29/08/1434", "dd/MM/yyyy", arSA);

    dateValue will be "7/7/2013".

    Best regards

    • Marked as answer by Min Zhu Monday, July 22, 2013 7:43 AM
    Monday, July 15, 2013 8:16 PM