How to create a list with hours and minutes based on interval

In the days of VB.NET I used to use two drop-downs to determine the hour and the minute of an import via the following manner:

For i As Integer = 0 To 23
ddlHour.Items.Add(New ListItem(String.Format("{0:0#}", i), i.ToString()))
Next
For i As Integer = 0 To 45 Step 15
ddlMinute.Items.Add(New ListItem(String.Format("{0:0#}", i), i.ToString()))
Next

I’ve had a play with the TimeSpan object and found an easy way to print the hours and mins using c#

private List LoadHours(int NumberOfMinutes)
        {
            var ts = TimeSpan.Zero;
            var ts_end =TimeSpan.FromDays(1);

            var lstReturn = new List();

            while (ts<ts_end)
            {
                lstReturn.Add(ts.ToString(@"hh\:mm"));

                //continue
                ts = ts.Add(TimeSpan.FromMinutes(NumberOfMinutes));
            }
            return lstReturn;
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s