JAC JAC - 2 months ago 24
C# Question

How convert Gregorian date to Persian date?

I want to convert a custom Gregorian date to Persian date in C#.
For example, i have a string with this contents:

string GregorianDate = "Saturday, October 24, 2013";


Now i want to have:


string PersianDate = پنج‌شنبه 2 آبان 1392 ;


or


string PersianDate = 1392/08/02


Thanks

Answer

Use the PersianCalendar:

string GregorianDate = "Thursday, October 24, 2013";
DateTime d = DateTime.Parse(GregorianDate);
PersianCalendar pc = new PersianCalendar();
Console.WriteLine(string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)));

I changed the day to Thursday since that was the 24th of October, 2013.