Home > Software engineering >  How to run an application with If statement within specific datetime?
How to run an application with If statement within specific datetime?

Time:03-14

The first If statement worked but other didn't work i don't really know why. also ELSE statement worked, but seems like when i add days it dosen't function with me.

string today = "13/03/2022";
  if (today == DateTime.Now.ToString("dd/MM/yyyy"))
    Application.Run((Form) new Program.Notification.NotificationForm());
  if (today == DateTime.Now.AddDays(1).ToString("dd/MM/yyyy"))
    Application.Run((Form) new Program.Notification.NotificationForm());
  if (today == DateTime.Now.AddDays(2).ToString("dd/MM/yyyy"))
    Application.Run((Form) new Program.Notification.NotificationForm());
  else
    Application.Run((Form) new Program.Notif2.NotificationForm());

so if u still don't understand what i need is, i need the application form to run after a specefic date like above 13/03/2022 so after todays date which is 13/03/2022 has passed and 14/03/2022 has passed ( because i added 1 day ) and 15/03 ( because i added 2 day ) has passed then anything else run the ( else ) statement. i test by changing my pc date and time by days FIRST IF statement worked and ELSE statement worked. but the added days one dosent work. any alternative way?

CodePudding user response:

Change the comparrison type and reduce the if statements to if/else

DateTime today = new DateTime(2022, 03, 13);
        
if (today >= DateTime.Today && today <= DateTime.Today.AddDays(2))              
    Application.Run((Form)new Program.Notification.NotificationForm());
else
    Application.Run((Form)new Program.Notif2.NotificationForm());

CodePudding user response:

string today = "13/03/2022";
  if (today == DateTime.Now.ToString("dd/MM/yyyy"))
    Application.Run((Form) new Program.Notification.NotificationForm());
  else if (today == DateTime.Now.AddDays(1).ToString("dd/MM/yyyy"))
    Application.Run((Form) new Program.Notification.NotificationForm());
  else if (today == DateTime.Now.AddDays(2).ToString("dd/MM/yyyy"))
    Application.Run((Form) new Program.Notification.NotificationForm());
  else
    Application.Run((Form) new Program.Notif2.NotificationForm());

From your post, I think the if setences should be constructed like above.

As for the adding days testing, since you defined today as 13/03/2022, you'll need to set pc time to

  • 12/03/2022 to test 1day
  • 11/03/2022 to test 2day.

Your variables might need to change names, because DateTime.Now is the real date of today (in your system), while today, the string variable, is set to be a certain date, which is confusing.

  • Related