string with Persian Calendar format was not recognized as a valid DateTime

629 Views Asked by At

I use Persian Date (Iranian Date Format or Jalali Calendar) in my program.

and when i use this:

string A = "1396/2/30";
string Test = String.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(A));

I get the following error:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code. Additional information: String was not recognized as a valid DateTime.

1

There are 1 best solutions below

6
On

Create a fa-IR CultureInfo and then try using DataTime.ParseExact() instead of Convert.ToDateTime().

Update

var persianCultureInfo = new CultureInfo("fa-IR");
persianCultureInfo.DateTimeFormat.Calendar = new PersianCalendar();
string dateString = "1396/02/30";
DateTime MyDateTime = DateTime.ParseExact(dateString, "yyyy/MM/dd", persianCultureInfo);