Hi

I am trying to populate a listview with all the hours of the day in 30 minute intervals.

I can doing very basically from 0 to 24 with the following code:

Private Sub CreateTimeStep()

        For i = 0 To 25 Step 0.5

            lstDays.Items.Add(i)

        Next
    End Sub

but what i would like to do is have it display in 24 hour format e.g. 00:00, 00:30 ...etc

I have tried using the date.time variable but it just doesnt work.

Dani AI

Generated

Good start from (step from midnight) and the fix you applied, . For a robust, culture-independent 24‑hour list avoid floating steps (Step 0.5) and generate times by integer minutes. That prevents rounding surprises and makes formatting explicit.

A compact VB.NET approach is to build a list of strings using minute increments and TimeSpan formatting (this produces "00:00", "00:30", ... "23:30"):

Dim times As New List(Of String)
For minutes As Integer = 0 To (24 * 60 - 30) Step 30
    times.Add(TimeSpan.FromMinutes(minutes).ToString("hh\:mm"))
Next

Populate your UI from that list. For a WinForms ListBox call Items.AddRange(times.ToArray()). For an ASP.NET ListView set DataSource = times and call DataBind() (or bind objects with Text/Value and set DataTextField/DataValueField if you need separate values).

Notes and pitfalls:

  • Use TimeSpan.FromMinutes or an integer-minute loop to avoid floating-point imprecision.
  • To force 24-hour formatting with DateTime use ToString("HH:mm"); for TimeSpan use the escaped colon format hh\:mm. See Microsoft docs on custom date/time format strings and custom TimeSpan format strings.
  • If users see AM/PM, the display is coming from culture settings—use an explicit format string (and, if needed, CultureInfo) to ensure consistent 24‑hour output.

Recommended Answers

All 3 Replies

Try this:

Dim d As Date = "00:00:00"
        For x As Integer = 0 To 47
            ListBox1.Items.Add(d.TimeOfDay.ToString)
            d = d.AddMinutes(30)
        Next

this looks like just the thing - many thanks - will update when tested

hi

your code worked fine but i just needed to get rid of the last zeros so used the following

Dim d As DateTime = FormatDateTime("00:00")
For x As Integer = 0 To 47
lstDays.Items.Add(FormatDateTime(d.TimeOfDay.ToString, DateFormat.ShortTime))
d = d.AddMinutes(30)
Next
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.