Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to make a simple program to convert time given in seconds to hh:mm:ss format. But for some particular input values it produces an incorrect time format. This is what I have tried:

Public Class Form1
    Dim Hours, Minutes, Seconds As Integer

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        lblHours.Text = "00"
        lblMinutes.Text = "00"
        lblSeconds.Text = "00"
        txtTimeSeconds.Text = ""
        txtFormattedTime.Text = ""
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate1.Click
        Seconds = Integer.Parse(txtTimeSeconds.Text)
        Hours = Seconds / 3600
        Seconds = Seconds Mod 3600
        Minutes = Seconds / 60
        Seconds = Seconds Mod 60

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

        txtFormattedTime.Text = Hours.ToString.PadLeft(2, "0"c) & ":" & Minutes.ToString.PadLeft(2, "0"c) & ":" & Seconds.ToString.PadLeft(2, "0"c)
    End Sub
End Class

It works when the input value is 30:

Enter image description here

It does not work when the input value is 31:

Enter image description here

What have I done wrong? How can I fix this problem?

like image 968
Failed_Noob Avatar asked Jan 15 '12 10:01

Failed_Noob


People also ask

How do you convert seconds to HH mm SS?

To convert seconds to HH:MM:SS :Multiply the seconds by 1000 to get milliseconds. Pass the milliseconds to the Date() constructor. Use the toISOString() method on the Date object. Get the hh:mm:ss portion of the string.

How do you convert seconds to HH mm SS format in Excel?

As with Excel, the first step to converting elapsed second to time is to divide the value by 86400. To format the cells for mm:ss, select Format > Number > More Formats > More date and time formats from the Menu.

How do you convert seconds to HH mm SS format in Python?

Use the timedelta() constructor and pass the seconds value to it using the seconds argument. The timedelta constructor creates the timedelta object, representing time in days, hours, minutes, and seconds ( days, hh:mm:ss.ms ) format. For example, datetime.


2 Answers

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)
like image 58
John Woo Avatar answered Oct 23 '22 04:10

John Woo


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.

like image 9
Hans Passant Avatar answered Oct 23 '22 02:10

Hans Passant