Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check specific hour

Tags:

date

c#

timer

I've searched here for a solution, but I can't make it work for me.

My problem is:

I have a rotine where a email is sent each 2 minutes, but now I need to check the hour to send this email only once for day.

I need something like this

if(hourNow == '18')
  send();

Can someone help me?

like image 495
Bruno Pinto Avatar asked Feb 22 '26 11:02

Bruno Pinto


2 Answers

The DateTime type has an Hour property that returns the hour of the day (0 - 23). You can use that:

if(DateTime.Now.Hour == 18)
    send();
like image 140
driis Avatar answered Feb 25 '26 00:02

driis


Try this:

var nowHour = DateTime.Now.Hour;

if(nowHour==18)
{
}

DateTime.Now gets current date and time, and .Hour get hour. You can do this with Day, Year, Month etc.

like image 23
Kamil Budziewski Avatar answered Feb 24 '26 23:02

Kamil Budziewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!