9

I need to convert date stored in database into Hijri and display the same in Arabic

I used the Culture to convert the date which it does but it still display date as English numbers

Example Gregorian Date = 19/01/2012 Its equivalent date in Hirji is 25/02/1433

Following code snippet converts but displays same as 25/02/1433 While i want it in Arabic numbers something like ٢٥/٠٢/٢٠١٢"

string sDate    
DateTime dtt = Convert.ToDateTime("19/01/2012");
CultureInfo ci = new CultureInfo("ar-SA");
sDdate = dtt.ToString("d", ci);

Is there a was it converts date to Hijri and display same as Arabic

I need this for a web project which i am developing in ASP.NET c#

Learning
  • 19,469
  • 39
  • 180
  • 373

5 Answers5

22

The CultureInfo class will not help you in either parsing or formatting the numbers to eastern Arabic ("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩") nor to western Arabic ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"). You have to manually convert it, Here is a little function which will do that for you in a neat way:

public string ConvertToEasternArabicNumerals(string input)
    {
        System.Text.UTF8Encoding utf8Encoder = new UTF8Encoding();
        System.Text.Decoder utf8Decoder = utf8Encoder.GetDecoder();
        System.Text.StringBuilder convertedChars = new System.Text.StringBuilder();
        char[] convertedChar = new char[1];
        byte[] bytes = new byte[] { 217, 160 };
        char[] inputCharArray = input.ToCharArray();
        foreach (char c in inputCharArray)
        {
            if (char.IsDigit(c))
            {
                bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));
                utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);
                convertedChars.Append(convertedChar[0]);
            }
            else
            {
                convertedChars.Append(c);
            }
        }
        return convertedChars.ToString();
    }

Now alter your code a little to look like this:

string sDate    
DateTime dtt = Convert.ToDateTime("19/01/2012");
CultureInfo ci = new CultureInfo("ar-SA");
sDate = ConvertToEasternArabicNumerals(dtt.ToString("dd/MM/yyyy", ci));

And things will work just fine. BTW, the code for the function was taken from here.

5

you should convert the HijriDate to a string, assume its name is stringHijriDate. Then use a function like below to convert its digits to arabic digit:

  public static string ToArabicNumber(this string inputString)
    {
        string[] arabicDigits = CultureInfo.GetCultureInfo("fa-IR").NumberFormat.NativeDigits;
        var arabicNumberBuilder = new StringBuilder();
        foreach (char c in inputString)
        {
            if (char.IsDigit(c))
                arabicNumberBuilder.Append(arabicDigits[int.Parse(c.ToString())]);
            else
                arabicNumberBuilder.Append(c);
        }
        return arabicNumberBuilder.ToString();
    }

the result string will be something like '٢٥/٠٢/٢٠١٢'

  • I prefer linq `string[] arabicDigits = CultureInfo.GetCultureInfo("fa-IR").NumberFormat.NativeDigits; var arabicNumberBuilder = new StringBuilder(); var result = string.Join("", inputString.Select(x => char.IsDigit(x) ? arabicDigits[int.Parse(x.ToString())] : x.ToString())); return result;` – Milad Oct 02 '17 at 06:02
1

I use DigitSubstitution to switch between Hindi(called Arabic sometimes) numbers and English numbers :

bool UseHindiNumbers;
//...
if (UseHindiNumbers)
  Thread.CurrentThread.CurrentCulture.NumberFormat.DigitSubstitution = DigitShapes.NativeNational;
else
  Thread.CurrentThread.CurrentCulture.NumberFormat.DigitSubstitution = DigitShapes.None;
Amer
  • 468
  • 1
  • 5
  • 15
0
Private Function DigitContext(ByVal Vl As String) As String
    Dim ks As String
    Dim c As Integer
    Dim t As Integer
    ks = "" 'reset the returned string
    For t = 1 To Vl.Length
        c = AscW(Vl.Substring(t - 1, 1))
        Select Case c
            Case &H30 To &H39
                ks = ks & ChrW(c + &H630)
            Case 46
                ks = ks & ChrW(1548) '&H060C 'ks = ks & ","
            Case Else
                ks = ks & Mid(Vl, t, 1)
        End Select
    Next
    Return ks

End Function
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
0

you will need to set page's UI culture to arabic

Junaid
  • 1,708
  • 16
  • 25
  • That will convert the date but wont display it as arabic "٢٥/٠٢/٢٠١٢" rather it displays it as 25/02/1433 (Which is a Hijri Calender date) – Learning Jan 19 '12 at 13:46
  • did you try that? UICulture to display item in selected culture. – Junaid Jan 19 '12 at 13:48
  • Yes, I have tried it with calender control which display month in Arabic but days in English – Learning Jan 19 '12 at 13:51