Program to convert time in seconds to hh:mm:ss format
Asked Answered
V

8

18

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?

Voltaic answered 15/1, 2012 at 10:39 Comment(0)
E
22

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)
Emblazonment answered 15/1, 2012 at 10:56 Comment(0)
S
9

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.

Slow answered 15/1, 2012 at 10:54 Comment(1)
Seems to me that this is the real answer, describing exactly what goes wrong and presenting a solution to it. +1!Kilogram
S
6

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")))
Showiness answered 27/11, 2013 at 14:43 Comment(0)
W
3

You are using integers to store your data but division gives you doubles. When converting it back to integers it gets rounded to the nearest round number. So 0.5 becomes 0 but 0.51 becomes 1.

Waltner answered 15/1, 2012 at 10:55 Comment(0)
F
2
Dim SecondsDifference as integer = 2500  
Dim hms = TimeSpan.FromSeconds(SecondsDifference)
Dim h = hms.Hours.ToString
Dim m = hms.Minutes.ToString
Dim s = hms.Seconds.ToString
MsgBox("Hour:" + h + " Min:" + m + " Sec:" + s)
Feeder answered 29/8, 2013 at 4:27 Comment(0)
S
1

For the txtformattedtime.text=... I think "ispan.tostring" would work as well.

Skiles answered 22/5, 2013 at 10:0 Comment(1)
Could you explain a bit more?Kilogram
I
1

I know this one has already been answered for a while now, but I thought I might share my solution to the problem at hand. If you place the number of seconds in a TimeSpan object, you can quite easily extract the days, hours, minutes, seconds and even fractional seconds directly using TimeSpan.toString() method. Using an identical form and object names, I used the following code to achieve the desired results.

Public Class Form1
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
    lblHours.Text = "00"
    lblMinutes.Text = "00"
    lblSeconds.Text = "00"
    txtTimeSeconds.Text = ""
    txtFormattedTime.Text = ""
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim tsSeconds = TimeSpan.FromSeconds(Convert.ToDouble(txtTimeSeconds.Text))
    lblHours.Text = tsSeconds.ToString("hh")
    lblMinutes.Text = tsSeconds.ToString("mm")
    lblSeconds.Text = tsSeconds.ToString("ss")
    txtFormattedTime.Text = tsSeconds.ToString("hh\:mm\:ss")
End Sub
End Class

Visit here for more information on the method used.

Izanami answered 24/12, 2015 at 20:31 Comment(0)
E
0
Function ToFormattedHour(ByVal seconds As Long) As String
    Dim hours As Long, minutes As Long

    hours = seconds \ 3600
    seconds = seconds Mod 3600

    minutes = seconds \ 60
    seconds = seconds Mod 60

    Return String.Format("{0}:{1}:{2}", hours, minutes, seconds)
End Function
Esquibel answered 7/1 at 23:49 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Merrel

© 2022 - 2024 — McMap. All rights reserved.