簡體   English   中英

檢查日期x是否早於某些天數

[英]Check if date x are older than some number of days

這是我在C#中的第一個項目。 我需要檢查日期x是否超過某些天數。

例:

if ( THIS_IS_SAVED_DATE_5_DAYS_AGO < OLDER_THAN_5_DAYS ) {

// Do this if saved date is more than 5 days

} 

感謝幫助。

您可以使用AddDays方法構建相對於當前日期的新日期,然后比較兩個日期:

DateTime x = ...
if (x < DateTime.Now.AddDays(-5)) 
{
    // x is older than 5 days
}

你可以這樣做:

if (x < x.AddDays(-1*days)) {

}

您可以減去日期並檢查差異( TimeSpan類型):

if ((DateTime.Now - THIS_IS_SAVED_DATE_5_DAYS_AGO).TotalDays < 5) {
    //Executed when not older than two days
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM