Reputation: 5037
I'm getting UTC time from devices that appear in arabic unicode. How can I convert this format to a DateTime object?
Here is an example of the date format: ٢٠١٤-١٢-٢٨T٢١:٤١:٥٨Z
For the curious, it should translate to: 2014/12/28 21:41:58
Upvotes: 1
Views: 1254
Reputation: 151690
Combining How to convert Arabic number to int? and How to create a .Net DateTime from ISO 8601 format:
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;
}
Yields 2014-12-28T21:41:58Z
and 2014-12-28 21:41:58Z
.
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.
Upvotes: 3