问题描述
我从出现在阿拉伯统一码中的设备获取UTC时间.如何将这种格式转换为DateTime对象?
I'm getting UTC time from devices that appear in arabic unicode. How can I convert this format to a DateTime object?
以下是日期格式的示例:٢٠١٤-١٢-٢٨T٢١:٤١:٥٨Z
Here is an example of the date format:٢٠١٤-١٢-٢٨T٢١:٤١:٥٨Z
出于好奇,它应该翻译为:2014/12/28 21:41:58
For the curious, it should translate to:2014/12/28 21:41:58
推荐答案
组合如何将阿拉伯数字转换为整数?和如何从ISO 8601格式创建.Net DateTime :
static void Main(string[] args)
{
string input = "٢٠١٤-١٢-٢٨T٢١:٤١:٥٨Z";
string output = ReplaceArabicNumerals(input);
var dateTime = DateTime.Parse(output, null, DateTimeStyles.AssumeUniversal);
Console.WriteLine(output);
Console.WriteLine(dateTime.ToString("u"));
Console.ReadKey();
}
public static string ReplaceArabicNumerals(string input)
{
string output = "";
foreach (char c in input)
{
if (c >= 1632 && c <= 1641)
{
output += Char.GetNumericValue(c).ToString();
}
else
{
output += c;
}
}
return output;
}
收益率 2014-12-28T21:41:58Z
和 2014-12-28 21:41:58Z
.
ReplaceArabicNumerals()
方法的说明:当它检测到阿拉伯语-印度数字(在代码点1632(0)和1641(9)之间)时,它请求字符的数值.这会将.NET可以解析的东阿拉伯数字转换为西阿拉伯数字.
Explanation of the ReplaceArabicNumerals()
method: when it detects an Arabic-Indic numeral (between code point 1632 (0) and 1641 (9)), it requests the numerical value of the character. This translates the East-Arabic numerals into West-Arabic ones that .NET can parse.
这篇关于将Unicode中的日期时间字符串(带有阿拉伯数字)转换为DateTime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!