![](/img/trans.png)
[英]How to check if a datetime is exactly a month before today´s date in c#
[英]C# , Trying to check expire dates with today's date
我要強調的是到期日,它在今天的21天之內似乎在起作用。 但是,這似乎也突出了明年的日期。 因此,例如:今天2/22/2017,突出顯示了2/28/2017的到期日期。 但這也突出了2018年2月29日,這是從現在開始的整年。 我如何獲得它還包括年度檢查。 現在只是幾天。 我在下面使用的代碼:
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;
// .....
}
我建議您使用TimeSpan來了解兩個日期之間的時差。 例如
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/zh-CN/library/system.timespan(v=vs.110).aspx
希望這段代碼對您有所幫助。 如果您已經設置了所需的到期日期。 那么我們將獲得今天的日期。
下面的代碼將檢索您希望的所有截止日期到今天的當前日期。
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.