Reputation: 330
In my code, I want to convert from the Gregorian calendar (ex: 2019/10/8) to the Persian calendar (ex: 1398/07/16). Here is my code:
public static string ToPersianDate(this DateTime t)
{
var pc = new PersianCalendar();
return $"{pc.GetYear(t)}/{pc.GetMonth(t)}/{pc.GetDayOfMonth(t)}";
}
but what I get is the same date I pass as input parameter!
I mean if I pass 2019/10/08 I will get 2019/10/08. It seems as if no conversion ever happened.
Upvotes: 0
Views: 609
Reputation: 5986
You could try the following code to convert Gregorian date to Persian date.
string GregorianDate = "2019/10/08";
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)));
Upvotes: 1