【问题标题】:Better logic to check Financial year dates更好的逻辑来检查财政年度日期
【发布时间】:2014-11-28 01:44:54
【问题描述】:

我已经有一个逻辑来检查输入的日期是否完全是财政年度日期

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;
    }
}

这样我检查输入的日期正好有 1 年的差异。但它不会检查用户是否准确输入了 startdate 01/07/20XX - endate 30/06/20XX。我可以硬编码和验证日期和月份,并检查输入年份之间的差异来实现这一点,但有没有更好的方法可以做到这一点?

【问题讨论】:

  • 这真的取决于你的听众。不同的国家有不同的年度报告期。
  • 当然可以,但是这里的财政年度日期例如:01/07/2013 -- 30/06/2014。目前,此应用程序未在任何其他国家/地区使用。但你是对的,为所有国家设计一个会很有趣
  • 您可以在资源文件中包含财政年度的日期。对于不同的国家,您需要创建不同的资源文件。 winform:msdn.microsoft.com/en-us/library/y99d1cd3.aspx webform:msdn.microsoft.com/en-us/library/vstudio/…
  • 假设这是用于某种查询的参数,您应该考虑消除所有不必要的日期部分(月和日)并简单地根据财政年度(或财政年度,无论你怎么称呼它)。所以,也许只是 FY 的下拉菜单,只有年份值。
  • 如果您实际上并不关心日期,为什么还要麻烦用户输入日期?就让他进入年份吧。

标签: c# c#-4.0 c#-3.0 c#-2.0


【解决方案1】:

我希望这会有所帮助

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.
}

或者,您可以要求用户仅输入开始日期,然后计算结束日期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多