for Loop

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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());
}
}
 
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"));
}
 
Back
Top