Home > Blockchain >  just a simple request here about Datetime.ToString im not into this but needed the assistant
just a simple request here about Datetime.ToString im not into this but needed the assistant

Time:11-23

#endif

            string today = "#datetime";
            if (today == DateTime.Now.ToString("dd/MM/yyyy"))
            {
                Application.Run(new ClipboardNotification.NotificationForm());
            }
            else
            {
                Application.Run(new ClipboardNotification2.NotificationForm());
            }

so apparently in this code above there is a method would run within only 1 IF statement after todays daily date has past, so I wanted to make it run after 2 days instead of 1 day so what should I put their? any help would be appreciated

CodePudding user response:

Compare the Dates with DateTime or TimeSpan which will be easier. Don't use strings for dates as they can't be compared (eg 11/23/2021 vs 23/11/2021). Try like this I hope its helps:

var date = new DateTime(2021,11,23);

if(date > DateTime.Today.AddDays(2))
{
      // do stuff
}

CodePudding user response:

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());
}

check if this works for you.

  • Related