gpt4 book ai didi

C# 添加项目,如果它们在日期之间

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

我创建了一个程序,如果项目在日期之间,则需要添加项目,这是我使用的代码:

if (day >= fromDay - 1 && day <= tillDay && month >= fromMonth - 1 && month <= tillMonth && year >= fromYear - 1 && year <= tillYear)
{
listBox1.Items.Add(OpenFiles[m_index].getFileName());
}

代码工作正常但有一个错误:它检查日、月和年是否在开始和停止之间。因此,即使您想添加从 19.02.2011 到 15.04.2011 的内容,它也不会添加或看到任何内容。请帮我解决这个问题。

最佳答案

你应该比较日期而不是日期的组成部分:

// Presumably you can determine these once... (possibly rename to earliestValid
// and latestValid, or something like that?)
DateTime from = new DateTime(fromYear, fromMonth, fromDay);
DateTime to = new DateTime(toYear, toMonth, toDay);

// Then for each candidate...
...
DateTime date = new Date(year, month, day);
if (date >= from && date <= to)
{
listBox1.Items.Add(...);
}

(当然,对于 date 类型而不是日期和时间,请查看 Noda Time :)

关于C# 添加项目,如果它们在日期之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7836256/

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