for Loop

G

Guest

Hi,

Is there is a way to loop through a time variable?

For instance we can loop using integers as follow:

for(int i=0; i<11; i++)
{
Debug.WriteLine(i.ToString());
}

How can I do the same for time and increment by a factor of 30 minutes?

Thanks,

Yama
 
C

Cathal Connolly [VB MVP]

the third parameter of the for loop is the step, so you can set it to be 30,
and then wrap the inner loop in an outer hour based loop i.e.

for(int iHour=1; iHour<=24; iHour++)
{

for(int iMin=0; iMin<=60; iMin+30)
{
Debug.WriteLine(iHour.ToString() + ":" + iMin.ToString());
}
}
 
C

Chris Dunaway

Here's an alternative:

DateTime start, stop;

for (start=DateTime.Now, stop=start.AddMinutes(240); start <
stop; start = start.AddMinutes(30))
{
Console.WriteLine(start.ToString("hh:mm tt"));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top