user380432
user380432

Reputation: 4779

C# loops through 24 hours every 30 minutes

I am wondering how I would loop through a datetime or any type of variable to go from 12:00AM to 11:59PM every 30 Mins?

So I need a variable that shows times in 12HR format (01:00PM, 09:00AM) and everytime I loop through it, to add 30 mins to the time? I then need to use this value in a string.

The time needs to start at 10:00AM

Upvotes: 9

Views: 14177

Answers (8)

McNiel Viray
McNiel Viray

Reputation: 101

        DateTime endDate = DateTime.Now;
        DateTime startDate = endDate.AddDays(-1);

        while (startDate.AddMinutes(30) <= endDate)
        {
            string sdate = startDate.ToString("yyyy-MM-dd HH:mm");
            string edate = startDate.AddMinutes(29).ToString("yyyy-MM-dd HH:mm");

            string display = string.Format("{0} - {1}", sdate, edate);

            Console.WriteLine(display);
            startDate = startDate.AddMinutes(30);
        }

Upvotes: 0

Omer Bokhari
Omer Bokhari

Reputation: 59678

var times = new List<string>();
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
for (var i = today; i < tomorrow; i = i.AddMinutes(30))
{
    times.Add(i.ToShortTimeString());
}

Upvotes: 1

Matthew Whited
Matthew Whited

Reputation: 22443

And there is always LINQ

var start = DateTime.Today;
var clockQuery = from offset in Enumerable.Range(0, 48)
                 select start.AddMinutes(30 * offset);
foreach (var time in clockQuery)
    Console.WriteLine(time.ToString("hh:mm tt"));

... LINQ + FUNC (for parameterized start)

Func<DateTime, IEnumerable<DateTime>> clockQuery = start =>
    from offset in Enumerable.Range(0, 48)
    select start.AddMinutes(30 * offset);
foreach (var time in clockQuery(DateTime.Today))
    Console.WriteLine(time.ToString("hh:mm tt"));

... or if you just want the TimeSpan offsets ...

var start = DateTime.Today;
var clockQuery = from offset in Enumerable.Range(0, 48)
                 select TimeSpan.FromMinutes(30 * offset);
foreach (var time in clockQuery)
    Console.WriteLine((start + time).ToString("hh:mm tt"));

Upvotes: 16

Jeff LaFay
Jeff LaFay

Reputation: 13360

Something like this should work for you:

    int workCount = 0;
    var timer = new System.Timers.Timer(1800000);   // every half hour
    timer.AutoReset = true;

    timer.Elapsed += (src, e) =>
    {
        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss"));

        if(++workCount == 48)
        {
            timer.Stop();
        }
    };

    timer.Start();

Upvotes: 0

JonWillis
JonWillis

Reputation: 3147

        DateTime time = new DateTime(2011,02,22,10,0,0);
        List<String> times = new List<string>();

        for (int i = 0; i < 48; i++)
        {
            time = time.AddMinutes(30);
            times.Add(time.ToString());
        }

This might do what you need

Upvotes: 0

Radu094
Radu094

Reputation: 28444

something like this?

DateTime timeloop = new DateTime(0);
timeloop = timeloop.Add(new TimeSpan(10, 00, 0)); //start at 10:00 AM

            for (int i = 0; i < 48; i++)
            {
                string time =timeloop.ToString("hh:mm tt");           //print it as 1:30 PM
                timeloop = timeloop.Add(new TimeSpan(0, 30, 0));      //add 30 minutes
                               }

Upvotes: 3

BrokenGlass
BrokenGlass

Reputation: 161012

You could use an extension method:

public static class DateTimeHelper
{
    public static IEnumerable<DateTime> GetHalfHours(this DateTime dt)
    {
        TimeSpan ts = TimeSpan.FromMinutes(30);
        DateTime time = dt;
        while(true)
        {
            yield return time;
            time.Add(ts);
        }
    }
}

Upvotes: 6

Chris Pfohl
Chris Pfohl

Reputation: 19074

DateTime can do simple arithmetic:

DateTime time = DateTime.Now;
time = time + TimeSpan.FromMinutes(1);

Causes time to be incremented by one minute.

You can use a Timer class and increase the DateTime by whatever amount of time is appropriate once per tick. If exactness is important here there are other, more appropriate timer classes.

There are other static methods on the TimeSpan class as well!

Upvotes: 2

Related Questions