Afsaneh Daneshi
Afsaneh Daneshi

Reputation: 330

Converting from Gregorian calendar to the Persian calendar is not working

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

Answers (1)

Jack J Jun- MSFT
Jack J Jun- MSFT

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

Result: enter image description here

Upvotes: 1

Related Questions