#endif
string today = "#datetime";
if (today == DateTime.Now.ToString("dd/MM/yyyy"))
{
Application.Run(new ClipboardNotification.NotificationForm());
}
else
{
Application.Run(new ClipboardNotification2.NotificationForm());
}
很明顯,在上面的這段代碼中,有一個方法會在今天的每日日期過去后僅在 1 個 IF 陳述句中運行,所以我想讓它在 2 天而不是 1 天后運行,那么我應該把它們放什么?任何幫助,將不勝感激
uj5u.com熱心網友回復:
比較日期DateTime或TimeSpan哪個會更容易。不要strings用于日期,因為它們無法進行比較(例如 11/23/2021 與 23/11/2021)。像這樣嘗試我希望它有幫助:
var date = new DateTime(2021,11,23);
if(date > DateTime.Today.AddDays(2))
{
// do stuff
}
uj5u.com熱心網友回復:
const int intervalDays = 2; // desired interval
string dateString = "30/11/2021";
DateTime convertedDate = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
TimeSpan gap = DateTime.Now - convertedDate;
if (Math.Abs(gap.Days) < intervalDays)
{
Console.WriteLine("logic for less than 2 days");
//Application.Run(new ClipboardNotification.NotificationForm());
}
else
{
Console.WriteLine("logic for greater than or equal to 2 days");
//Application.Run(new ClipboardNotification2.NotificationForm());
}
檢查這是否適合您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/363629.html
