none
How to calculate the hours, minutes, and seconds in a number of seconds?

    Question

  • I want to convert seconds such as 254565443 seconds to hours, minutes, and seconds and thought if I could remove the decimal and the numbers behind it and not change the number in front of the decimal then it would work.

     

    dim seconds = 254565443

    dim Hour

    dim minute

    Hour = (seconds/3600).drop off the decimal without changing the part in front

    seconds = seconds - (hour * 3600)

    minute = (seconds/60).drop off the decimal without changing the part in front

    seconds = seconds - (minute * 60)

    textbox1.text = hour.tostring & " Hours Left " & minute.tostring & " Minutes Left " & seconds.tostring & " Seconds Left"



    Thursday, June 30, 2011 7:18 PM

Answers

  • Use TimeSpan class:

    Dim totalSeconds As Integer = 254565443
    Dim ts As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
    Console.WriteLine("Hours:{0}, Minutes:{1}, Seconds{2}", ts.Hours, ts.Minutes, ts.Seconds)
    Console.ReadLine()
    



    Mitja
    Thursday, June 30, 2011 7:44 PM

All replies

  • Use TimeSpan class:

    Dim totalSeconds As Integer = 254565443
    Dim ts As TimeSpan = TimeSpan.FromSeconds(totalSeconds)
    Console.WriteLine("Hours:{0}, Minutes:{1}, Seconds{2}", ts.Hours, ts.Minutes, ts.Seconds)
    Console.ReadLine()
    



    Mitja
    Thursday, June 30, 2011 7:44 PM
  • How do i display the timespan? How do i display it using a label?
    Tuesday, July 19, 2011 1:03 PM
  • Depends on the version of .Net. 

     

    Prior to 4.0

     

            Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)

     

    For 4.0

     

            Label1.Text = ts.ToString("hh\:mm\:ss")

     

    where ts is a timespan.

     


    Serial Port      Random      Microsoft® Community Contributor 2011
    Tuesday, July 19, 2011 1:28 PM