Program to convert time in seconds to hh:mm:ss format

There is class in .NET called TimeSpan which makes your code easy and elegant.

Example:

dim iSecond as double = 0 'Total number of seconds
Dim iSpan As TimeSpan = TimeSpan.FromSeconds(iSecond)

lblHours.Text = iSpan.Hours.ToString.PadLeft(2, "0"c)
lblMinutes.Text = iSpan.Minutes.ToString.PadLeft(2, "0"c)
lblSeconds.Text = iSpan.Seconds.ToString.PadLeft(2, "0"c)

txtFormattedTime.Text = iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" & _
                        iSpan.Minutes.ToString.PadLeft(2, "0"c) & ":" & _
                        iSpan.Seconds.ToString.PadLeft(2, "0"c)

Visual Basic has two division operators, / and \. The / operator produces a result that's of type Double. You calculate 31 / 60 = 0.51666... You next assign that result to an Integer, that requires rounding. Thus producing 1, not 0.

You want to use the \ operator, the integer division operator. It truncates the result.


I hope this code will be useful

Dim ts As TimeSpan = TimeSpan.FromSeconds(227) 'or --> Dim ts As New TimeSpan(0, 0, 0, 227, 0)

Dim mydate As DateTime = New DateTime(ts.Ticks)
MessageBox.Show(mydate.ToString(("HH:mm:ss")))