Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, DateTime Format A date and time format string defines the text representation of a DateTime value that results from a formatting operation. Learn More,

Display Gregorian calendar date with Arabic digits instead of Hijri Calendar date in c#?

Description:we have a multilingual website in which we have event start date and end date which is correctly displayed for English language version of the page but when we go to its Arabic version of the page, it basically convert the date to Hijri calendar date.
Currently we are using following code in which we are just getting the dates from database and format it as shown


string startDate= sourceEvent?.Fields.EventStart.ToString("dd.MM.yy");
string endDate= sourceEvent?.Fields.EventEnd.ToString("dd.MM.yy");

Output for English page
25.05.22 - 28.05.22
Output of Arabic page:
24.10.43 - 27.10.43
instead the Arabic page output we required:
۲۵.۰۵.۲۲ - ۲۸.۰۵.۲۲

Posted by: | Posted on: May 30, 2022

1 answers

Replies

3

You need to make following changes to your code and it will fix the problem.
First of all you need to use the CultureInfo.InvariantCulture which make sure that you will have same date with both English and Arabic.

string startDate= sourceEvent?.Fields.EventStart.ToString("dd.MM.yy" , CultureInfo.InvariantCulture);
string endDate= sourceEvent?.Fields.EventEnd.ToString("dd.MM.yy" , CultureInfo.InvariantCulture);

Secondly you need create the following method to convert the English digits to Arabic digits in case we have Arabic culture

private string ConvertNumerals(string input)
{
if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "ar")
{
return input.Replace('0', '\u06f0').Replace('1', '\u06f1').Replace('2', '\u06f2').Replace('3', '\u06f3').Replace('4', '\u06f4')
.Replace('5', '\u06f5').Replace('6', '\u06f6').Replace('7', '\u06f7').Replace('8', '\u06f8').Replace('9', '\u06f9');
}
else return input;
}
So your final code will look like the following one, where you can call the ConvertNumerals method.

string startDate= ConvertNumerals(sourceEvent?.Fields.EventStart.ToString("dd.MM.yy" , CultureInfo.InvariantCulture));
string endDate= ConvertNumerals(sourceEvent?.Fields.EventEnd.ToString("dd.MM.yy" , CultureInfo.InvariantCulture));

Replied by: | Replied on: Jun 01, 2022



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview