0

In my c# code, I am getting an Arabic date from a function, I need to convert this date to English format (UK/US). I tried with the following code

string startdate = "٢٠١٩-٠٩-٠٣";

var dateTime = DateTime.ParseExact(
  startdate, 
 "d MMMM yyyy", 
  CultureInfo.InvariantCulture);

it throws exception:

System.FormatException: 'String was not recognized as a valid DateTime.'

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
atc
  • 13
  • 3
  • 2
    Note that `ParseExact` tries to create a `DateTime` object which does not have any format (at all). It simply stores any DateTime as a `long` value, `Ticks`. I'm also not sure that `InvariantCulture` is able to understand Arabic dates. An Arabic culture would probably be a better option here. – ProgrammingLlama Oct 02 '19 at 07:48
  • 2
    I have to go with John on this one. The description of `CultureInfo.InvariantCulture` even mentions that it's "associated with the English language" – Powerlord Oct 02 '19 at 07:50
  • The dupe I linked to shows how to do exactly this... parse a date from Eastern Arabic numerals. – haldo Oct 02 '19 at 08:22

3 Answers3

1

You can try convert digits (which are Easten Arabic) into Western Arabic ones (i.e. into 0..9):

  string startdate = @"٢٠١٩-٠٩-٠٣";

  string translated = string.Concat(startdate.Select(c => char.GetNumericValue(c) < 0 
     ? c.ToString()                          // Character, keep intact  
     : char.GetNumericValue(c).ToString())); // Digit! we want them be 0..9 only

  // Arabic language has right to left order, that's why pattern is "yyyy-M-d"
  DateTime dateTime = DateTime.ParseExact(
    translated, // note, not startdate
   "yyyy-M-d", 
    CultureInfo.InvariantCulture, 
    DateTimeStyles.AssumeLocal);

  // English, Great Britain culture
  Console.Write(dateTime.ToString("d MMMM yyyy", CultureInfo.GetCultureInfo("en-GB")));

Outcome:

  3 September 2019

Fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Try specifying the culture of the string you are trying to parse:

var culture = CultureInfo.GetCultureInfo("YOUR CULTURE CODE"); // Probably "ar-SA"
var startDate = "٢٠١٩-٠٩-٠٣";
var dateTime = DateTime.ParseExact(startDate, "d MMMM yyyy", culture);

You can find the culture codes here:

http://www.csharp-examples.net/culture-names/

Joas
  • 1,796
  • 1
  • 12
  • 25
  • It might be worth mentioning that `"d MMMM yyyy"` is the expected format of `startDate`, not the format of `dateTime` (although `DateTime` doesn't have a format, many people make the mistake of thinking it does). – ProgrammingLlama Oct 02 '19 at 07:56
  • It doesn't work. it shows culture doesn't exist. I had tried this early – atc Oct 02 '19 at 08:01
0

Add this namespace using System.Globalization;

string arabicTextDate= "٢٠١٩-٠٩-٠٣";
     var str = arabicTextDate
    .Replace('\u0660','0')
    .Replace('\u0661','1')
    .Replace('\u0662','2')
    .Replace('\u0663','3')
    .Replace('\u0664','4')
    .Replace('\u0665','5')
    .Replace('\u0666','6')
    .Replace('\u0667','7')
    .Replace('\u0668','8')
    .Replace('\u0669','9');
    DateTime dt=DateTime.ParseExact(str, "yyyy-MM-dd", CultureInfo.InvariantCulture);

Follow this link for fiddle. Code With Example

enter image description here

Muhammad Asad
  • 1,772
  • 16
  • 23