C# 添加介于日期之间的项目

发布于 2024-12-11 04:05:25 字数 467 浏览 0 评论 0原文

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

if (day >= fromDay - 1 && day <=tilDay && ; 月份 >= fromMonth - 1 & 月份 <= 直到月份 && 年 >= fromYear - 1 && 年 <= 直到年份) { listBox1.Items.Add(OpenFiles[m_index].getFileName()); 代码

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

I've created a program will need to add items if the items are between a date, this is the code I use:

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

The code works fine but it has a fault in it: It checks if the day, month and year is between the start and stop.
So even you want to add something from the 19.02.2011 till the 15.04.2011, it doens't add or see anything. Please help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随心而道 2024-12-18 04:05:25

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

// 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(...);
}

当然,对于日期类型而不是日期和时间,请查看野田时间 :)

You should compare dates rather than the components of the dates:

// 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(...);
}

(Of course, for a date type instead of date and time, have a look at Noda Time :)

旧时光的容颜 2024-12-18 04:05:25
DateTime fromTime;
DateTime toTime;
DateTime currentTime = DateTime.Now;

if (currentTime >= fromTime && currentTime <= toTime)
{
 //to do some stuff
}
DateTime fromTime;
DateTime toTime;
DateTime currentTime = DateTime.Now;

if (currentTime >= fromTime && currentTime <= toTime)
{
 //to do some stuff
}
ゃ懵逼小萝莉 2024-12-18 04:05:25

它将以这种方式工作:

var dateFrom = new DateTime(yearFrom, monthFrom, dayFrom);
var dateTo = new DateTime(yearTo, monthTo, dayTo);
var actualDate = new DateTime(year, month, day);
if ((dateFrom < actualDate) && (actualDate < dateTo))
{
    // Do something
}

如果您单独比较日期的各个部分,它将不起作用(正如您已经发现的那样:-D)

It will work this way:

var dateFrom = new DateTime(yearFrom, monthFrom, dayFrom);
var dateTo = new DateTime(yearTo, monthTo, dayTo);
var actualDate = new DateTime(year, month, day);
if ((dateFrom < actualDate) && (actualDate < dateTo))
{
    // Do something
}

If you compare the parts of the dates seperately, it won't work (as you already found out :-D )

我们的影子 2024-12-18 04:05:25

为什么不根据 from 和 toDate 以及实际日期创建一个日期并执行此操作>

if(fromDate < date && date <= tillDate)
{
}

Why don't you create a date out of the from and toDate and from the actual date and do this>

if(fromDate < date && date <= tillDate)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文