I have a problem converting Gregorian calendar dates to Persian(Hijri calendar) using default system "en-GB" culture in my ASP.NET MVC Application. I used the globalization tag in my web.config so the default culture would be "en-GB":
<system.web>
...
<globalization uiCulture="en-GB" culture="en-GB" />
...
</system.web>
The conversion will happen for any date except for these exact dates that I listed them below and will throw "String was not recognized as a valid DateTime.” error.
The exact dates that will throw exception are:
- 22/07/(any year)
- 22/09/(any year)
- 19/05/(any year)
- 20/05/(any year)
- 21/05/(any year)
A part of my sample code:
using System.Globalization;
...
...
public static PersianCalendar PC = new PersianCalendar();
...
//I'm using this method for conversion
public static DateTime GregorianToPersian(DateTime date)
{
...
string stringDate = string.Format("{0}/{1}/{2} {3}:{4}:{5}"
, PC.GetDayOfMonth(date), PC.GetMonth(date), PC.GetYear(date)
, PC.GetHour(date), PC.GetMinute(date), PC.GetSecond(date));
return DateTime.Parse(stringDate); //error!
}
Hope you guys can help me out with. Thanks.