DateTimePicker和实际日期计算 [英] DateTimePicker and Actual Date Calculations

查看:183
本文介绍了DateTimePicker和实际日期计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行此程序,这里有此产品列表(它们都在数据库中,并通过数据集连接到该程序),每个产品都有各自的到期日期.

现在,我有了这个DateTimePicker,它是一个链接到数据库的日历,您可以从中更改失效日期(它也显示实际的失效日期).我需要知道的是,当某个产品快要到期时(例如,如果x产品到期= <10天,则msgbox(产品x快要到期")),如何使我的程序发出警告.

如果您不明白我的意思,请说出来,我会更好地解释.

I''m doing this program where I have this list of products (they are all in a database and are connected to the program through a dataset), and each one has a respective expiration date.

Now, I have this DateTimePicker, which is a Calendar linked to a database and from it you can change the expiration date (it also shows the actual one). What I need to know is how do I get the program to give me a warning when there is a product almost expiring ( something like, if x product expiration =< 10 days then msgbox("product x is almost expiring")).

If you didn''t understand what I meant, say it and I''ll explain it better.

推荐答案

您可以简单地使用DateTime的AddDays方法并检查像:expirationDate < DateTime.Now.AddDays(10)

http://msdn.microsoft.com/en-us/library/system.datetime. adddays.aspx [ ^ ]

祝您好运!
You can simply use the AddDays method of DateTime and check like: expirationDate < DateTime.Now.AddDays(10)

http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx[^]

Good luck!


要添加到EF Nijboers答案中,请不要重复使用DateTime.Now.制作一份副本,并使用它代替:
To add to EF Nijboers answer, don''t repeatedly use DateTime.Now. Take a copy, and use that instead:
DateTime expiryDate = DateTime.Now.AddDays(10);
foreach(Product p in products)
   {
   if (p.ExpiryDate >= expiryDate)
      {
      ...
      }
   }

这样做的原因是每次使用DateTime.Now时,它将使用当前日期和时间创建一个新实例.在此示例中,它没有什么区别(除了浪费一点时间和内存),但是当一天经过两次检查时,它可能会导致间歇性错误.
最好使用DateTime.现在应尽量避免使用此时间.

The reason for this is that each time to use DateTime.Now, it creates a new instance with the current date and time. In this example it makes no difference (except to waste a little time and memory), but it can cause intermittent bugs when the day rolls over between checks.
It is good practice to use DateTime.Now as few times as possible to avoid this.


这对我来说有点困惑.谢谢您的帮助:thumbsup:
That''s a bit confusing to me Griff. Thanks for the help though :thumbsup:


这篇关于DateTimePicker和实际日期计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆