user29892: I am working on a system that starts a process based of a set time value (startHour). The user sets the start hour and can only select values from 00-23. I have been asked to start the same process at 6 hour intervals so that it occurs 4 times during the day. 
My initial though is to add three more variable, secondStartHour, thirdStartHour and fourthStartHour. secondStartHour would be assigned a value 6 hours ahead of the startHour, thirdstartHour a value 12 hours ahead of startHour and fourthStartHour would be assigned a value 18 hours ahead of start hour. I could then use any of the startHour variables to trigger the process. 
I wrote some code (below) that will work but I'm wondering if there is a better, more efficient way of doing it. 
//set up secondStartHour 6 hours ahead of startHour
if (startHour <= 5)
{
    secondStartHour = startHour - 18;
}
else
{
    secondStartHour = startHour + 6;
}

//set up thirdStartHour 12 hours ahead of startHour
if (startHour <= 11)
{
    thirdStartHour = startHour - 12;
}
else
{
    thirdStartHour = startHour + 12;
}

//set up fourthStartHour 18 hours ahead of startHour
if (startHour <= 17)
{
    fourthStartHour = startHour - 6;
}
else
{
    fourthStartHour = startHour + 18;
}

I had to write the code in the way I did above because I am working with a 24 hour clock so in some cases adding time to a startHour will actually reduce the value. By this I mean if startHour is 23 (23:00) and I need to add 6 hours to it to get secondStartHour the result will be 5 (05:00) so I can't simply write code to say secondStartHour = startHour + 6. The code just seems too long winded and messy but I cant think of a better way to write it. I could use a switch case statement but then I would have 24 cases with assignments for each of the new variables in each case so that seems worse, any ideas would be appreciated. 

user31105: Instead of comparing, you can use the modulo operator:
 secondStartHour = (startHour +  6) % 24;

And similar for the others:
 thirdStartHour  = (startHour + 12) % 24;
 fourthStartHour = (startHour + 18) % 24;

While xxxStartHour >= 24, 24 will be removed until it is within range [0, 24).
If you need an offset, you also can add some hours of course after the module (I don't fully understand your example which can have values >= 24.
Also, if you need to do the same task every 6 hours, you can use with one time and 'restart' it every 6 hours (and maybe taking summer/winter time into account).

user4143: If you think about it logically you actually only have 6 start hours. Things can only start at a time between 0 and 5 am inclusive.  Everything else is a multiple of 6 hours on from that start.  Any time provided that's past 5am is actually a future time of an earlier start time.
So you can just "modulus" the given time so it is one of the first 6 hours of the day:
startHour = startHour % 6;

And then add 6 each time for the other hours:
secondStartHour = startHour + 6;
thirdStartHour = startHour + 12;
fourthStartHour = startHour + 18;

So your hour arrangements can only be:
0  6  12  18
1  7  13  19
2  8  14  20
3  9  15  21
4 10  16  22
5 11  17  23

If you provide the hour 15 as a start time, 15 % 6 is 3 (divide 15 by 6, you get 2 with 3 remaining), so you are in the time pattern "3 9 15 21".

29892: <p>I am working on a system that starts a process based of a set time value (startHour). The user sets the start hour and can only select values from 00-23. I have been asked to start the same process at 6 hour intervals so that it occurs 4 times during the day. </p>

<p>My initial though is to add three more variable, secondStartHour, thirdStartHour and fourthStartHour. secondStartHour would be assigned a value 6 hours ahead of the startHour, thirdstartHour a value 12 hours ahead of startHour and fourthStartHour would be assigned a value 18 hours ahead of start hour. I could then use any of the startHour variables to trigger the process. </p>

<p>I wrote some code (below) that will work but I'm wondering if there is a better, more efficient way of doing it. </p>

<pre><code>//set up secondStartHour 6 hours ahead of startHour
if (startHour &lt;= 5)
{
    secondStartHour = startHour - 18;
}
else
{
    secondStartHour = startHour + 6;
}

//set up thirdStartHour 12 hours ahead of startHour
if (startHour &lt;= 11)
{
    thirdStartHour = startHour - 12;
}
else
{
    thirdStartHour = startHour + 12;
}

//set up fourthStartHour 18 hours ahead of startHour
if (startHour &lt;= 17)
{
    fourthStartHour = startHour - 6;
}
else
{
    fourthStartHour = startHour + 18;
}
</code></pre>

<p>I had to write the code in the way I did above because I am working with a 24 hour clock so in some cases adding time to a startHour will actually reduce the value. By this I mean if startHour is 23 (23:00) and I need to add 6 hours to it to get secondStartHour the result will be 5 (05:00) so I can't simply write code to say secondStartHour = startHour + 6. The code just seems too long winded and messy but I cant think of a better way to write it. I could use a switch case statement but then I would have 24 cases with assignments for each of the new variables in each case so that seems worse, any ideas would be appreciated. </p>
 31105: <p>Instead of comparing, you can use the modulo operator:</p>

<pre><code> secondStartHour = (startHour +  6) % 24;
</code></pre>

<p>And similar for the others:</p>

<pre><code> thirdStartHour  = (startHour + 12) % 24;
 fourthStartHour = (startHour + 18) % 24;
</code></pre>

<p>While xxxStartHour >= 24, 24 will be removed until it is within range [0, 24).</p>

<p>If you need an offset, you also can add some hours of course after the module (I don't fully understand your example which can have values >= 24.</p>

<p>Also, if you need to do the same task every 6 hours, you can use with one time and 'restart' it every 6 hours (and maybe taking summer/winter time into account).</p>
 4143: <p>If you think about it logically you actually only have 6 start hours. Things can only start at a time between 0 and 5 am inclusive.  Everything else is a multiple of 6 hours on from that start.  Any time provided that's past 5am is actually a future time of an earlier start time.</p>

<p>So you can just "modulus" the given time so it is one of the first 6 hours of the day:</p>

<pre><code>startHour = startHour % 6;
</code></pre>

<p>And then add 6 each time for the other hours:</p>

<pre><code>secondStartHour = startHour + 6;
thirdStartHour = startHour + 12;
fourthStartHour = startHour + 18;
</code></pre>

<p>So your hour arrangements can only be:</p>

<pre><code>0  6  12  18
1  7  13  19
2  8  14  20
3  9  15  21
4 10  16  22
5 11  17  23
</code></pre>

<p>If you provide the hour 15 as a start time, 15 % 6 is 3 (divide 15 by 6, you get 2 with 3 remaining), so you are in the time pattern "3 9 15 21".</p>
