there is a c # program, in it, for example, once a day at 12:00 a certain action must be performed, and in other hours another action.

at 12:00 he did not want to work out.

how to implement it correctly?

DayToday = new DateTime(daynow1.Year, daynow1.Month, daynow1.Day, 12, 0, 0); if (DayToday == DateTime.Now) { Что-то делается } else { Что-то другое делается } 
  • when is this code called? - Grundy
  • the code is called on a timer once an hour, but it doesn’t matter the code only works on else, and at 12:00 it should work on if but does not want - Andrey Kalinin
  • make a condition not at the exact time. and for an hour and put a flag. or call once a minute - des1roer
  • since you do not guarantee that the timer starts at exactly 00:00 each hour, then the test may or may not take place, depending on the start time of the timer - Grundy

1 answer 1

 var timeNow = DateTime.Now; if (timeNow.Hour == 12 && timeNow.Minute == 00) { } 

But this code does not guarantee the execution of the then block at 12:00 if it is called less than once per minute.