题
我使用这些代码来转换当前日期时间,并将其减去用户在文本框中输入的日期时间。但是转换的时候会报错。
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","");
将日期时间从字符串转换为日期时间时,此行会出现错误:DateTime d1 = DateTime.Parse(str);
请帮我解决这个问题。
提前致谢
解决方案 4
我解决了这个问题。它是由于在几个月内出现了,所以如果你想要减去彼此的两个日期时间,你应该转换到Gregorian日历,那么你可以减去它们并查看结果。
以下是代码:
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
. 其他提示
无需在此处解析日期。
编辑:
最初我只想用这个答案指出op处理中的错误。
但我认为完全答案会更好(尽管延迟:))
是的,我知道中间日期表示看起来不像波斯日期。但这不是需要的。此代码在用户投入的日期和日期之间提供适当的差异。
.string userInput = "1394/02/31";
System.DateTime date = System.DateTime.Today;
System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();
int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
System.DateTime currentDate = new System.DateTime(year, month, 1);
currentDate = currentDate.AddDays(day - 1);
// validation omitted
System.String[] userDateParts = userInput.Split(new[] { "/" }, System.StringSplitOptions.None);
int userYear = int.Parse(userDateParts[0]);
int userMonth = int.Parse(userDateParts[1]);
int userDay = int.Parse(userDateParts[2]);
System.DateTime userDate = new System.DateTime(userYear, userMonth, 1);
userDate = userDate.AddDays(userDay - 1);
System.TimeSpan difference = currentDate.Subtract(userDate);
只需在ToDateTime
类上使用PersianCalendar
方法:
PersianCalendar p = new PersianCalendar();
DateTime now = DateTime.Now;
DateTime dateT = p.ToDateTime(now.Year, now.Month, now.Day, 0, 0, 0, 0);
.
然后以此格式获取您的字符串,刚执行:
string str = dateT.ToShortDateString();
. 您的实际问题似乎在于解析用户以波斯日历格式输入的日期。所以,如果用户输入 1391/2/31
你希望能够解析它。今天(2012 年 5 月 19 日)是 1391/2/30
所以你最终想要显示类似的东西 1 day remaining
.
这个问题有 之前被问过. 。然而,那里接受的答案只是尝试使用
string persianDate = "1390/02/07";
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime persianDateTime = DateTime.ParseExact(persianDate,
"yyyy/MM/dd", persianCulture);
这不适用于像这样的约会 1391/2/31
. 。所以我认为你必须自己实现解析。
不实施任何错误检查,并假设用户始终使用该格式 yyyy/mm/dd
你可以使用:
// Convert Persian Calendar date to regular DateTime
var persianDateMatch = System.Text.RegularExpressions.Regex.Match(
persianTargetTextBox.Text, @"^(\d+)/(\d+)/(\d+)$");
var year = int.Parse(persianDateMatch.Groups[1].Value);
var month = int.Parse(persianDateMatch.Groups[2].Value);
var day = int.Parse(persianDateMatch.Groups[3].Value);
var pc = new System.Globalization.PersianCalendar();
var target = pc.ToDateTime(year, month, day, 0, 0, 0, 0);
var difference = target - DateTime.Today;
differenceLabel.Text = difference.TotalDays.ToString("0");
您需要检查正则表达式是否真正找到任何匹配项。你还必须检查是否 pc.ToDateTime
不会抛出错误。不幸的是,似乎不存在类似的东西 DateTime.TryParse
为波斯历。
无论如何,您不需要将今天解析为波斯日历,您只需要解析用户输入。
你也可能对此有兴趣 波斯语库 - 使用日期、日历和日期选择器, ,尽管这篇文章很老了(2007 年)。
这里是您的朋友的地方:
datetime构造函数(int32,int32,int32,calendar) < / p>
这是您需要的确切代码:
PersianCalendar p = new System.Globalization.PersianCalendar();
DateTime today = DateTime.Today;
DateTime pDate = new DateTime(p.GetYear(today), p.GetMonth(today), p.GetDayOfMonth(today), p);
DateTime d2 = DateTime.Parse(textBox9.Text);
// THIS NEEDS TO BE EXPLAINED
textBox10.Text = (d2 - pDate).ToString().Replace(".00:00:00","");
.
最终,你想做什么?您期望在textBox10
中的结果是什么?如果您想要两个日期之间的天数,那么只是做生意划线,你将在几天内得到差异。也许如果你进一步解释了你想要做的事情,而不是你如何尝试这样做,我们可以帮助你进一步帮助你。
编辑:刚才意识到Comecme所说的话。导致问题的线路在这里:
DateTime d2 = DateTime.Parse(textBox9.Text);
.
相反,我们需要更改它也使用文化信息:
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime d2 = DateTime.Parse(textBox9.Text, persianCulture)
.
编辑:您是正确的,它只允许在波斯格式中解析日期,但不会根据特定日历验证。为了正确地实现这一工作,您将不得不手动解析日期并使用Persiancalendar提供的新日期时间:
DateTime d2;
if(Regex.IsMatch(textBox9.Text, "^dddd/dd/dd$"))
{
d2 = new DateTime(
int.Parse(textBox9.Substring(0,4)),
int.Parse(textBox9.Substring(5, 2)),
int.Parse(textBox9.Substring(8, 2)),
p);
}
.
您只需确保输入肯定匹配您指定的格式,否则无法准确一致地解析DateTime。
您可以使用此代码:
DateTime today = DateTime.Today; // excludes h/m/s
DateTime userdate = DateTime.Parse(textBox9.Text);
.
如果从另一个从另一个upssstract yoursivean。
TimeSpan diff = today - userDate;
.
您可以使用此时间跨度的属性来向他们展示您喜欢的方式。
管理转换表单/到波斯日历,在此处有一个很好的解决方案:
.NET如何将波斯语(Jalali Calendar)的日期字符串解析为DateTime对象?
参考:
这里是我使用的代码:
public DateTime ConvertPersianToEnglish(string persianDate)
{
string[] formats = { "yyyy/MM/dd", "yyyy/M/d", "yyyy/MM/d", "yyyy/M/dd" };
DateTime d1 = DateTime.ParseExact(persianDate, formats,
CultureInfo.CurrentCulture, DateTimeStyles.None);
PersianCalendar persian_date = new PersianCalendar();
DateTime dt = persian_date.ToDateTime(d1.Year, d1.Month, d1.Day, 0, 0, 0, 0, 0);
return dt;
}
. 这可以更轻松地使用 noda time :
using System.Globalization;
using NodaTime;
using NodaTime.Text;
...
CultureInfo culture = CultureInfo.CreateSpecificCulture("fa-IR");
CalendarSystem calendar = CalendarSystem.GetPersianCalendar();
LocalDate template = new LocalDate(1, 1, 1, calendar);
LocalDatePattern pattern = LocalDatePattern.Create("yyyy/M/d", culture, template);
LocalDate result = pattern.Parse("1391/2/30").Value;
// if you need a DateTime, then:
DateTime dt = result.AtMidnight().ToDateTimeUnspecified();
. 您还可以使用以下.NET库使用PersianCalendar等于DateTime:
试试这个代码: 编辑该代码的代码由“grzegorz w”发布,但他的代码生成错误,例如,当用户输入日期=“1394/06/31”和错误是“年,月份和日参数描述一个未计入的DateTime”
以下代码是OK:
string userInput = "1394/02/31";
System.DateTime date = System.DateTime.Today;
System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();
int year = p.GetYear(date);
int month = p.GetMonth(date);
int day = p.GetDayOfMonth(date);
System.DateTime currentDate = new System.DateTime(year, month, day,p);
System.String[] userDateParts = userInput.Split(new[] { "/" }, System.StringSplitOptions.None);
int userYear = int.Parse(userDateParts[0]);
int userMonth = int.Parse(userDateParts[1]);
int userDay = int.Parse(userDateParts[2]);
System.DateTime userDate = new System.DateTime(userYear, userMonth, userDay,p);
System.TimeSpan difference = currentDate.Subtract(userDate);
.