Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Display 1

Status
Not open for further replies.

trk1616b

MIS
May 25, 2005
20
0
0
US
I am trying to calculate a time to be displayed in a label (Minutes & Seconds) from a given # of seconds. So, if the seconds returned is 125, I want the label to read 2:05.

I'm having a hard time due to integer rounding. Below is what I have right now:

s_timemn = lDuration / 60
s_timesec = lDuration - (s_encodetimemn * 60)
s_time = s_timemn & ":" & s_timesec
lbl_time.Text = s_time

The problem with this is that if the given seconds is 31 seconds, the label will display "1:-29" instead of "0:31". I've played with several things, but can't get a good version. Any ideas?

Thanks
 
You could try something like the following. The code includes two different methods- the first uses integer division and the mod operator, the second uses the TimeSpan function.

Code:
  Private Seconds As Integer = 29
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim h As Integer
    Dim m As Integer
    Dim s As Integer

    Dim str As String

    s = Seconds Mod 60
    m = Seconds \ 60

    str = m.ToString + ":" + s.ToString("00")
    MessageBox.Show(str)

    str = TimeSpan.FromSeconds(Seconds).Minutes.ToString + ":" + TimeSpan.FromSeconds(Seconds).Seconds.ToString("00")
    MessageBox.Show(str)

  End Sub

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top