How convert Gregorian date to Persian date?

JAC picture JAC · Jun 6, 2015 · Viewed 37.3k times · Source

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

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

Now i want to have:

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

or

string PersianDate = 1392/08/02

Thanks

Answer

Alioza picture Alioza · Jun 6, 2015

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)));