0

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?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
דניאל
  • 183
  • 9
  • Isn't it (`1442/25/ربيع الثاني`) a *Hijri* date? https://en.wikipedia.org/wiki/Islamic_calendar – Dmitry Bychenko Apr 08 '21 at 15:15
  • It's supposed to be ar-AR, I'll fix it, but still the date obtained is completely wrong – דניאל Apr 08 '21 at 15:17
  • `ar-AE` stands for arabic language, United Arab Emitrates which looks quite OK; however `ar-AR` is arabic language, *Argentina*? Please, look yourself: `Console.Write(CultureInfo.GetCultureInfo("ar-AR").EnglishName);` – Dmitry Bychenko Apr 08 '21 at 15:22
  • But if the date coming from the database is a Gregorian calendar and I am trying to convert it to a future date according to the Islamic calendar? – דניאל Apr 08 '21 at 15:27
  • 1
    Date comes as `DateTime` which can be *represented* in different formats (Gregorian calendar, Hijri, Dangun calendar etc.). In your case - arabic culture - the date has been *represented as string* in Hijri – Dmitry Bychenko Apr 08 '21 at 15:30
  • Yes, now I understand that. I will look for a converter that will detect this and make this change between the different calendars, Thx :) – דניאל Apr 08 '21 at 15:37
  • instead of looking for converters, I suggest create a *custom culture* which will be `ar-AE` except Date and Time formatting – Dmitry Bychenko Apr 08 '21 at 15:59

1 Answers1

1

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      
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • For developers who are looking for the short way to display text in Arabic and in addition to also see the Gregorian date, the recommended option is to choose this culture:- ar-AE. For developers who looking for a way to work with the both calendars (Gregorian calendar and Islamic calendar) and not just with the text in Arabic then the solution you offer is definitely more relevant. Thank for your answer – דניאל Apr 08 '21 at 16:21