C#将DateTime.Now转换为带有阿拉伯数字的阿拉伯月份 [英] C# Convert DateTime.Now to Arabic months with arabic digits

查看:296
本文介绍了C#将DateTime.Now转换为带有阿拉伯数字的阿拉伯月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在回答我的问题,并提供了案例...
我的规范,我需要这样的日期

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

我尝试过

  • Code project to solve this!
  • Stackoverflow references 1
  • stackoverflow references 2
  • ArabTream2000 website (Arab community)

上述问题未按预期工作,


  1. 它没有正确更改月份,例如用英语显示月份

  2. 年份是用英文数字显示,例如用英文数字2019显示年份

这浪费了我的时间...周围有什么代码可以解决我的问题?

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

推荐答案

首先,我是C#入门人员,因此,如果对代码进行了编辑,请发布并继续进行优化。

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,

案例1

当我在行下使用时,我得到 1441محرم24

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

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

现在请密切注意 MMMM ar-SA ,以显示称为محرم

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

问题在这里,您会用英语获得 1441

Problems here is you get 1441 in English!

所以

案例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();
        }

代码组合:

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

这篇关于C#将DateTime.Now转换为带有阿拉伯数字的阿拉伯月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆