C# 检查财政年度日期的更好逻辑
我已经有了一个逻辑来检查输入的日期是否正好是财政年度日期C# 检查财政年度日期的更好逻辑,c#,c#-4.0,c#-3.0,c#-2.0,C#,C# 4.0,C# 3.0,C# 2.0,我已经有了一个逻辑来检查输入的日期是否正好是财政年度日期 if (DateTime.IsLeapYear(endate.Year)) { if (totalDay != 366) { error("Please make sure start and end dates are financial year dates.<br />E.g.<br /> Start Date:01/07/2012 <br /> End Date
if (DateTime.IsLeapYear(endate.Year))
{
if (totalDay != 366)
{
error("Please make sure start and end dates are financial year dates.<br />E.g.<br /> Start Date:01/07/2012 <br /> End Date :30/06/2013");
return;
}
}
else
{
if (totalDay != 365)
{
error("Please make sure start and end Dates are Financial year dates.<br />E.g.<br /> Start Date:01/07/2012 <br /> End Date :30/06/2013");
return;
}
}
if(DateTime.IsLeapYear(endate.Year))
{
如果(totalDay!=366)
{
错误(“请确保开始和结束日期为财政年度日期。
例如开始日期:2012年7月1日
结束日期:2013年6月30日”);
返回;
}
}
其他的
{
如果(totalDay!=365)
{
错误(“请确保开始和结束日期为财政年度日期。
例如开始日期:2012年7月1日
结束日期:2013年6月30日”);
返回;
}
}
这样,我检查输入的日期是否正好相差1年。但它不会检查用户是否准确输入了起始日期01/07/20XX-结束日期30/06/20XX。我可以硬编码和验证日期和月份,并检查输入年份之间的差异来实现这一点,但有更好的方法吗?我希望这会有所帮助
DateTime starDate = TextToDate(txtStartDate.Text); //01/07/2013
// or startDate = DateTimePicker1.Value;
DateTime endDate = TextToDate(txtEndDate.Text); //30/06/2014
// or endDate = DatetTimePicker2.Value
DateTime calculatedEndDate = startDate.AddYears(1).AddDays(-1);
// 01/07/2013 .AddYears(1) ==> 01/07/2014 .AddDays(-1) ==> 30/06/2014
// Note for above line:
// if you will be using the start date and end date in an sql query
// remove the call to function AddDays(-1)
// for display purposes, keep the function AddDays(-1)
if(endDate != calculatedEndDate)
// note: if DateTimePicker is returning time component, then you will need to compare each (year, month and day)
{
// show error message
}
else
{
// dates are 1 year apart.
}
或者,您可以要求用户只输入开始日期,然后计算结束日期。这取决于您的受众。不同的国家有不同的年度报告期。当然,但这里的财政年度日期是:2013年7月1日至2014年6月30日。目前,这个应用程序还没有在任何其他国家使用。但是你是对的,为所有国家设计一个会很有趣。你可以在资源文件中包含财政年度的日期。对于不同的国家,您需要创建不同的资源文件。WiFrase:WebFrase:假设这是针对某种查询的参数,您应该考虑消除所有不必要的日期部分(月和日),并简单地基于财政年度(或财政年度,无论您称之为什么)进行查询。所以,也许只是FY的一个下拉列表,只有年份值。如果你不在乎日期,为什么还要麻烦用户输入日期?就让他进入这一年吧。