在C#中解析阿拉伯语日期 [英] Parse arabic date in c#

查看:101
本文介绍了在C#中解析阿拉伯语日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在编写的应用程序中,我想解析使用c#的阿拉伯语格式的特定日期。例如,日期可能如下所示:٣٠.١٢.١٩٨٩

In an application that I'm writing I want to parse a specific date which is in the arabic language in c#. For example the date could look like this: ٣٠.١٢.١٩٨٩

但是我想要此输出:1989年12月30日

But i want this output: 30.12.1989

我的问题是如何在c#中执行此操作以从字符串中获取DateTime对象。

My question is how to do that in c# to get a DateTime object out of this string.

有人可以告诉我该怎么做吗?

Can anyone tell me how to to this?

非常感谢。

推荐答案

东部阿拉伯数字不受 DateTime 解析方法的支持,它们仅接受阿拉伯数字

Eastern Arabic numerals does not supported by DateTime parsing methods, they only accepts Arabic numerals.

另一方面,<一种href = https://msdn.microsoft.com/zh-cn/library/system.char.getnumericvalue rel = nofollow> char.GetNumericValue 方法非常有用,它可以将数字Unicode字符的浮点表示形式表示为 double ,对于东方阿拉伯数字也非常成功。

On the other hand, char.GetNumericValue method is quite good to get a floating-point representation of a numeric Unicode character as a double which perfectly successful for Eastern Arabic numerals as well.

如果您的str ing始终是基于这些数字的 dd.MM.yyyy 格式,您可以使用分割字符串。并获取它们这些字符的数值,解析为这些部分的整数,在 DateTime(年,月,日) 构造函数,并使用 dd.MM.yyyy获取它的字符串表示形式格式,并具有使用公历作为 日历属性,例如 InvariantCulture

If your string is always dd.MM.yyyy format based on those numerals, you can split your string with . and get their numeric values from those character, parse to integer those parts, use them in a DateTime(year, month, day) constructor and get it's string representation with dd.MM.yyyy format with a culture that using Gregorian Calendar as a Calendar property like InvariantCulture.

var s = "٣٠.١٢.١٩٨٩";
var day =   Int32.Parse(string.Join("",
                        s.Split('.')[0].Select(c => char.GetNumericValue(c)))); // 30
var month = Int32.Parse(string.Join("",
                        s.Split('.')[1].Select(c => char.GetNumericValue(c)))); // 12
var year =  Int32.Parse(string.Join("",
                        s.Split('.')[2].Select(c => char.GetNumericValue(c)))); // 1989

var dt = new DateTime(year, month, day);
Console.WriteLine(dt.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture)); // 30.12.1989

在这里 演示

或者,您可以创建自己的 Dictionary< char,char> 结构,并可以将映射的西方阿拉伯字符替换为东方阿拉伯字符。

As an alternative, you can create your own Dictionary<char, char> structure and you can replace Eastern Arabic characters mapped with Western Arabic characters.

var mapEasternToWestern = new Dictionary<char, char>
{ 
    {'٠', '0'}, 
    {'١', '1'}, 
    {'٢', '2'}, 
    {'٣', '3'}, 
    {'٤', '4'}, 
    {'٥', '5'}, 
    {'٦', '6'}, 
    {'٧', '7'}, 
    {'٨', '8'}, 
    {'٩', '9'}
};

这篇关于在C#中解析阿拉伯语日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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