Search code examples
c#entity-frameworkdatetimerazorasp-net-mvc-1

ToLongDateString() showing wrong date in Arabic


In my ASP NET MVC Entity Framework project I pass from database date and time information. When I display the date in the Arabic format CultureInfo("ar-AR") I get an incorrect date from what is entered in the database.

My code so far:

datetime_created.ToLongDateString();
// The date I pass is:- 2020-12-10
// CultureInfo("en-US") in en-US culture it displays the date as it is in a database:- Thursday, December 10, 2020
// CultureInfo("ar-AR") in ar-AE culture it displays another date:- 1442/ربيع الثاني/25

Any ideas why this happens?


Solution

  • When you work with ar-AE culture - Arabic language, United Arab Emirates you represent given DateTime as a string according to this culture. Here, it is Hijri calendar.

    However, you can easily create your own custom culture, which will be ar-AE except date representation:

      // The custom (myArabic) culture will be "ar-AE" one except date formatting 
      CultureInfo myArabic = CultureInfo.GetCultureInfo("ar-AE").Clone() as CultureInfo;
    
      // Let's use InvariantCulture DateTime rules 
      //TODO: specify all date time format settings here 
      myArabic.DateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;
    
      DateTime test = new DateTime(2020, 12, 10);
    
      Console.Write(test.ToString("G", myArabic));
    

    Outcome:

     12/10/2020 00:00:00