简体   繁体   English

C#,尝试用今天的日期检查过期日期

[英]C# , Trying to check expire dates with today's date

I am trying to highlight expiration dates when its within 21days of today's date, it seems to be working for the most part. 我要强调的是到期日,它在今天的21天之内似乎在起作用。 However, it seems to be highlighting next years date also. 但是,这似乎也突出了明年的日期。 so for example : Today 2/22/2017 , it is highlighting expire dates of 2/28/2017. 因此,例如:今天2/22/2017,突出显示了2/28/2017的到期日期。 But it is also highlighting 2/29/2018, which is a FULL year from now. 但这也突出了2018年2月29日,这是从现在开始的整年。 How do I get it to also include year check. 我如何获得它还包括年度检查。 Right now its just days. 现在只是几天。 Code I am using below: 我在下面使用的代码:

var now = DateTime.Now;
var expirationDate = DateTime.Parse(row.Cells[2].Value.ToString());
var TwentyOneDays = expirationDate.AddDays(-21);

var TenDays = expirationDate.AddDays(-10);

if (now > TwentyOneDays && now < expirationDate)
{
     row.DefaultCellStyle.BackColor = Color.LightSkyBlue;
     row.Cells[2].Style.BackColor = Color.Yellow;

     // .....
}

I suggest you using TimeSpan to know the difference between two dates. 我建议您使用TimeSpan来了解两个日期之间的时差。 eg 例如

DateTime expirationDate = DateTime.Now.AddDays(21);
DateTime now = DateTime.Now;

TimeSpan diff = expirationDate - now;

int days = diff.Days; // This would give 20

https://msdn.microsoft.com/en-us/library/system.timespan(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.timespan(v=vs.110).aspx

I hope this code help/s you. 希望这段代码对您有所帮助。 if you are already set your desired expired date. 如果您已经设置了所需的到期日期。 then we will get today's date. 那么我们将获得今天的日期。

The code below will retrieve all your desired expired date to current date today. 下面的代码将检索您希望的所有截止日期到今天的当前日期。

private void getexpireddate()
{
try
{
MySqlConnection con = new MySqlConnection(conn);
connect.Open();
string sql="SELECT expireddate FROM data1 where date(date) = date(now())";
MySqlDataAdapter da = new MySqlDataAdapter(sql, con);
DataTable ds = new DataTable();
da.Fill(ds);
datagrid.DataSource=ds;

}
catch(Exception ee)
{
Console.WriteLine(ee.Message);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM