-2

Am answering my questions, and providing the case... My Specification, i need the date to be like this,

hijri date example

I tried

The problem of the above did not work as expected,

  1. Either it did not change the month properly, e.g showing a month in English
  2. The year was in English numbers, e.g showing year in English digits 2019

It wasted my time... any code around to solve my problem?

shareef
  • 9,255
  • 13
  • 58
  • 89
  • 1
    You should show the problematic code that isn't working, along with any input, expected output, actual output, and errors/exceptions. – Rufus L Mar 20 '19 at 17:06
  • @RufusL thanks, as i stated, `The problem of the above it did not work as expected, either it did not change the month properly or the year was in English numbers etc..` i did try all code in the references – shareef Mar 20 '19 at 17:34
  • Maybe you could be more specific. What is culture invariant value, what you want, what you got by built-in approach. All provided by HTML or pictures. You could remove useless refs and unnecessary sentences. – Yarl Mar 20 '19 at 17:45

2 Answers2

1

First of all, Am C# starter, so if there is edit for the code, I posted please go ahead and optimize it.

By reading and trying the code, ( BTW very old posts here and there!) And after referring to عرض الوقت والتاريخين الهجري والميلادي في برنامجك سي شارب

I beginning to read the problem more clearly,

Case 1

When i used below line i got 1441 محرم 24

DateTime.Now.ToString("yyyy MMMM dd", new System.Globalization.CultureInfo("ar-SA"))

Now keep an eye on MMMM and ar-SA specifically to show the month called محرم.

Problems here is you get 1441 in English!

So

Case 2

To take this line one step further i used the convert to Arabic numbers method... This code shall change any digit to arabic/hindi charset...

public static 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();
        }

Code combined:

DateHelper.ConvertToEasternArabicNumerals(DateTime.Now.ToString("yyyy MMMM dd", new System.Globalization.CultureInfo("ar-SA")));
shareef
  • 9,255
  • 13
  • 58
  • 89
1

I think this is better handled by using date formatting for what it can do and then custom formatting the numbers that it cannot handle. From other SO answers about formatting numbers correctly it's easier to directly access the digits. But in this case we can use the desired culture directly.

    static void Main(string[] _)
    {
        DateTime dtm = new DateTime(2020, 1, 1, 0, 0, 0);

        var culture = new CultureInfo("ar-KW");

        var daymonth = dtm.ToString("dddd , MMMM, ", culture);
        var day = ToNativeNumberString(dtm.Day, 2, culture);
        var year = ToNativeNumberString(dtm.Year, 0, culture);

        var fulldate = $"{day}, {daymonth} {year}";

        Console.WriteLine(fulldate);
    }

    static string ToNativeNumberString(int number, int leftPadding, CultureInfo culture)
    {
        StringBuilder sb = new StringBuilder();

        while (number > 0)
        {
            var digit = number % 10;

            sb.Append(culture.NumberFormat.NativeDigits[digit]);

            number /= 10;
        }

        while (sb.Length < leftPadding)
        {
            sb.Insert(0, culture.NumberFormat.NativeDigits[0]);
        }

        return sb.ToString();
    }
MikeJ
  • 1,299
  • 7
  • 10