在C#中解析阿拉伯日期? [英] Parsing Arabic date in C#?

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

问题描述

我在解析日期时遇到问题,该程序被阿拉伯日期绊倒了。由于某些原因, DateTime.TryParse()输出此文化的垃圾。这是说明我的问题的示例代码:

I ran into a problem parsing dates, the program got tripped up by Arabic dates. For some reason DateTime.TryParse() outputs garbage for this culture. Here is example code that illustrates my problem:

var culture = CultureInfo.CreateSpecificCulture("ar");

DateTime date;

if (DateTime.TryParse(
    "15/01/16",
    culture,
    DateTimeStyles.None,
    out date))
{
    Console.WriteLine("TryParse with Arabic culture: " + date);
}

if (DateTime.TryParseExact(
    "15/01/16",
    culture.DateTimeFormat.ShortDatePattern, // dd/MM/yy
    culture,
    DateTimeStyles.None,
    out date))
{
    Console.WriteLine("TryParseExact with Arabic short date pattern and culture: " + date);
}

if (DateTime.TryParseExact(
    "15/01/16",
    culture.DateTimeFormat.ShortDatePattern, // dd/MM/yy
    CultureInfo.InvariantCulture,
    DateTimeStyles.None,
    out date))
{
    Console.WriteLine("TryParseExact with Arabic short date pattern and invariant culture: " + date);
}

输出为:

TryParse with Arabic culture: 1995-06-13 00:00:00
TryParseExact with Arabic short date pattern and culture: 1995-06-13 00:00:00
TryParseExact with Arabic short date pattern and invariant culture: 2016-01-15 00:00:00

仅最后一个版本有效,我不知道为什么前两个版本失败。我可以理解 TryParse 失败的原因是,它无法确定使用哪种模式。但是在 TryParseExact 中,我指定了确切的格式,但仍然无法正确解析。我不知道为什么会这样。

Only the last version works, and I can't figure out why the first two versions fail. I could understand TryParse failing because it couldn't figure out which pattern to use; but in TryParseExact I specify the exact format, and it still fails to parse properly. I can't figure out why this happens.

推荐答案

第三个实际上不是阿拉伯语。这是一种与文化无关的文化,可以随时在任何.NET应用程序中使用。这就是为什么它在 TryParseExact 中返回当前日期时间值的原因。 参阅

The third one actually not arabic. it's a culture-insensitive culture which can be always use in any .NET application. That's why it's returning the current datetime value in TryParseExact. Refer

阿拉伯文化的默认日历未使用格里高利历。它使用农历(UmAlQuraCalendar)。因此,您的日期被视为农历,并转换回公历。

Arabic culture not using the Gregorian calendar for it's default calendar. It's using the Lunar calendar (UmAlQuraCalendar). So your date is considered as lunar date and converted back to Gregorian.

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

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