将阿拉伯数字字符串转换为datetime对象在c# [英] convert arabic date string to datetime object in c#

查看:122
本文介绍了将阿拉伯数字字符串转换为datetime对象在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



DateTime是以这种格式出现的,是阿拉伯语:



14-يوليه-2015 04:44:51م



,我无法在c#中的datetime对象中转换它。



我已经尝试了这些迄今为止。

  DateTime latest = Convert .ToDateTime(latestTables.Result.StaticTable [i] .dtUpdTime,新的CultureInfo(en-US)); 

DateTime latest = DateTime.ParseExact(latestTables.Result.StaticTable [i] .dtUpdTime,dd-t hh:mm:ss yyyy-MMM,CultureInfo.InvariantCulture);

DateTime latest = Convert.ToDateTime(latestTables.Result.StaticTable [i] .dtUpdTime,新的CultureInfo(ar-SA));


解决方案

尝试将阿拉伯数字字符串粘贴到我的答案中发现由于RTL问题,角色有时被重新排序。特别是使用格式 d-MMMM-yyyy hh:mm:ss tt 格式化的 DateTime 将具有初始 d - 部分移动到最后(向左),这正是问题出现的字符串(例如从右到左:月,年,时间,AM / PM)说明者,然后天似乎很奇怪)。所以我假设你的字符串真的使用更自然的格式 d-MMMM-yyyy hh:mm:ss tt



您遇到的问题是您需要使用正确的日历。您尝试使用 ar-SA 文化,但默认日历是Hirji日历。您需要使用具有正确月份名称的公历。要更改日历,您需要创建一个新的 CultureInfo 并选择正确的日历。在您的情况下,您似乎需要在 OptionalCalendars 属性的索引5处使用本地化日历:

  var cultureInfo = CultureInfo.CreateSpecificCulture(ar-SA); 
cultureInfo.DateTimeFormat.Calendar = cultureInfo.OptionalCalendars [5];

然后,您可以使用此代码解析您的字符串(我以不同的方式创建字符串)当我将其粘贴到浏览器中时,将其更改):

  var dateTimeString = new String(new [] {
' 1',
'4',
' - ',
'ㄧ',
'و',
'ل',
'ي' ,
'و',
' - ',
'2',
'0',
'1',
'5',
'',
'0',
'4',
':',
'4',
'4',
':',
'5',
'1',
'',
'م'
});
var dateTime = DateTime.Parse(dateTimeString,cultureInfo);

生成的 DateTime 是:

 
2015-07-14T16:44:51

如果要使用 DateTime.ParseExact 格式为 d-MMMM-yyyy hh:mm:ss tt

  var dateTime = DateTime.ParseExact(dateTimeString,d-MMMM-yyyy hh:mm:ss tt,cultureInfo); 

您还应考虑使用 DateTime.TryParse DateTime.TryParseExact ,以获得更好的代码流程,如果您遇到无效的格式化字符串。


I have date time in string format coming from server which I can't change.

DateTime is coming in this format and is in Arabic :

14-يوليه-2015 04:44:51 م

and I am unable to convert it in datetime object in c#.

I have tried these so far.

DateTime latest = Convert.ToDateTime(latestTables.Result.StaticTable[i].dtUpdTime, new CultureInfo("en-US"));

DateTime latest = DateTime.ParseExact(latestTables.Result.StaticTable[i].dtUpdTime, "dd-t hh:mm:ss yyyy-MMM", CultureInfo.InvariantCulture); 

DateTime latest = Convert.ToDateTime(latestTables.Result.StaticTable[i].dtUpdTime, new CultureInfo("ar-SA"));

解决方案

Having tried to paste Arabic date strings into my answer I have discovered that characters are sometimes reordered due to RTL issues. In particular a DateTime formatted using the format d-MMMM-yyyy hh:mm:ss tt will have the initial d- part moved to the end (to the left) which is exactly how the string in your question appear (e.g. from right to left: month, year, time, AM/PM specifier and then day which seems weird). So I assume that your string really uses the more natural format d-MMMM-yyyy hh:mm:ss tt.

The issue that you are having is that you need to use the correct calendar. You have tried to use the ar-SA culture but the default calendar for this is a Hirji calendar. You need to use a Gregorian calendar with the correct month names. To change the calendar you need to create a new CultureInfo and choose the correct calendar. In your case it seems that you need to use the "Localized" calendar at index 5 of the OptionalCalendars property:

var cultureInfo = CultureInfo.CreateSpecificCulture("ar-SA");
cultureInfo.DateTimeFormat.Calendar = cultureInfo.OptionalCalendars[5];

You can then parse your string using this code (where I create the string in a way that does not change it when I paste it into my browser):

var dateTimeString = new String(new[] {
  '1',
  '4',
  '-',
  'ي',
  'و',
  'ل',
  'ي',
  'و',
  '-',
  '2',
  '0',
  '1',
  '5',
  ' ',
  '0',
  '4',
  ':',
  '4',
  '4',
  ':',
  '5',
  '1',
  ' ',
  'م'
});
var dateTime = DateTime.Parse(dateTimeString, cultureInfo);

The resulting DateTime is:

2015-07-14T16:44:51

If you want to use DateTime.ParseExact the format is d-MMMM-yyyy hh:mm:ss tt:

var dateTime = DateTime.ParseExact(dateTimeString, "d-MMMM-yyyy hh:mm:ss tt", cultureInfo);

You should also consider using DateTime.TryParse or DateTime.TryParseExact for better flow of code if you encounter an invalid formatted string.

这篇关于将阿拉伯数字字符串转换为datetime对象在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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