Bruno Pinto
Bruno Pinto

Reputation: 2013

Check specific hour

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?

Upvotes: 2

Views: 7908

Answers (6)

user3198952
user3198952

Reputation: 21

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.

Upvotes: 1

Niranjan
Niranjan

Reputation: 76

if (System.DateTime.Now.Hour == 18)

Upvotes: 1

usman
usman

Reputation: 89

Do it like this:

if (DateTime.Now.Hour == 18)
{
    // Code Here
}

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149078

You can use DateTime.Now to get the current date and time, and then the Hour property to get the current hour of the day:

if (DateTime.Now.Hour == 18)
    ...

Upvotes: 2

Kamil Budziewski
Kamil Budziewski

Reputation: 23107

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.

Upvotes: 2

driis
driis

Reputation: 164341

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

Upvotes: 10

Related Questions