gpt4 book ai didi

c# - 更好的逻辑来检查财政年度日期

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:12 24 4
gpt4 key购买 nike

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

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 年。但它不会检查用户是否准确输入了开始日期 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.
}

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

关于c# - 更好的逻辑来检查财政年度日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com