如何波斯日历日期字符串转换为日期时间?

问题描述:

我使用这些代码转换为当前日期时间再减去它的日期和时间,用户在文本框输入。但它同时转换提供了一个错误。

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

这行,同时从字符串转换日期时间为日期和时间将给予一个错误:日期时间D1 = DateTime.Parse(STR);

This line will accord an error while converting datetime from string to date time :DateTime d1 = DateTime.Parse(str);

请帮我解决这个问题。

在此先感谢

我解决了这个问题。这是因为在几个月的异常,所以如果你想减去对方两个日期时间,你都应该转换成公历,那么你就可以减去他们看到的结果。

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.

下面是代码:

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