I use the function below to convert Gregorian dates to Persian dates, but I’ve been unable to write a function to do the reverse conversion. I want a function that converts a Persian date (a string like “1390/07/18 12:00:00”) into a Georgian date.
public static string GetPdate(DateTime _EnDate)
{
PersianCalendar pcalendar = new PersianCalendar();
string Pdate = pcalendar.GetYear(_EnDate).ToString("0000") + "/" +
pcalendar.GetMonth(_EnDate).ToString("00") + "/" +
pcalendar.GetDayOfMonth(_EnDate).ToString("00") + " " +
pcalendar.GetHour(_EnDate).ToString("00") + ":" +
pcalendar.GetMinute(_EnDate).ToString("00") + ":" +
pcalendar.GetSecond(_EnDate).ToString("00");
return Pdate;
}
DateTimeis always in the Gregorian calendar, effectively. Even if you create an instance specifying a dfferent calendar, the values returned by theDay,Month,Yearetc properties are in the Gregorian calendar.As an example, take the start of the Islamic calendar:
It’s not clear how you’re creating the input to this method, or whether you should really be converting it to a string format. (Or why you’re not using the built-in string formatters.)
It could be that you can just use:
That will work for any
DateTimewhich has been created appropriately – but we don’t know what you’ve done before this.Sample:
Now if you’re not specifying the calendar when you create the
DateTime, then you’re not really creating a Persian date at all.If you want dates that keep track of their calendar system, you can use my Noda Time project, which now supports the Persian calendar: