How to convert Persian Calendar date string to DateTime?

c#, datetime

Solution

I solved that problem. it was because of exception in some months so if you want to minus two date times from each other you should convert both to the Gregorian calendar then you can minus them and see the result.

here are the codes :

PersianCalendar p = new PersianCalendar();
        string PersianDate1 = textBox1.Text;
        string[] parts = PersianDate1.Split('/', '-');
        DateTime dta1 = p.ToDateTime(Convert.ToInt32(parts[0]), Convert.ToInt32(parts[1]), Convert.ToInt32(parts[2]), 0, 0, 0, 0);
        string PersianDate2 = textBox2.Text;
        string[] parts2 = PersianDate2.Split('/', '-');
        DateTime dta2 = p.ToDateTime(Convert.ToInt32(parts2[0]), Convert.ToInt32(parts2[1]), Convert.ToInt32(parts2[2]), 0, 0, 0, 0);
        string s = (dta2 - dta1).ToString().Replace(".00:00:00", "");
        textBox3.Text = s; // Show the result into the textbox 

Problem

I use these codes for converting current datetime and minus it with the date time which users type in a textbox. But it gives an error while converting. ``` PersianCalendar p = new System.Globalization.PersianCalendar(); DateTime date = new DateTime(); date = DateTime.Parse(DateTime.Now.ToShortDateString()); int year = p.GetYear(date); int month = p.GetMonth(date); int day = p.GetDayOfMonth(date); string str = string.Format("{0}/{1}/{2}", year, month, day); DateTime d1 = DateTime.Parse(str); DateTime d2 = DateTime.Parse(textBox9.Text); string s = (d2 - d1).ToString().Replace(".00:00:00", ""); textBox10.Text = (d2 - d1).ToString().Replace(".00:00:00",""); ``` This line will accord an error while converting datetime from string to date time :`DateTime d1 = DateTime.Parse(str);` Please help me to solve this problem. Thanks in advance

Original source

Related problems