richard richard - 7 months ago 40
C# Question

Cannot convert Persian DateTime to Gregorian

i'm confused! I am using this code to convert a list of Persian datetimes to Gregorian but they don't seem to be converted:

static void Main(string[] args)
var lst = new List<string> { "1392/08/05", "1393/09/10", "1391/05/09" };
var persian = new PersianCalendar();
foreach (var persianDateTime in lst)
var d = persianDateTime.Split('/');
var gregorianDate = persian.ToDateTime(int.Parse(d[0]), int.Parse(d[1]), int.Parse(d[2]), 0, 0, 0, 0);

Result is :

enter image description here


You are correctly converting the date, but then you are calling ToShortDateString() which is using your current culture to display the value. As the documentation says:

The string returned by the ToShortDateString method is culture-sensitive.

You are using the Persian culture, so the output string is returned in Persian format.

Edit: to see the actual value, you can of course use the overload of DateTime.ToString() which accepts an IFormatProvider parameter and pass CultureInfo.InvariantCulture, or whichever culture you wish to use to format the value: