Seconds, Minutes, and Hours
Tuesday, March 13, 2012
Posted by Priya Yadav
I have a little function that converts seconds into other units. Let's take a look at it.
Let's take a closer look at this. First, I used the Mod operationto get the seconds. "Mod" finds the remainder when one number is divided byanother. In this case, when we divide our time elapsed by 60 (one minute), ourremainder is the number of seconds left over.
Then I did a similar process to find the minutes, only this time I had to divide the numberof seconds by 60 to get the total number of minutes. Again, I used the Modoperator to find the remainder (in this case, the number of leftover minutes).
Finally, the number of hours is the number of seconds divided by 3600. Once again, I usedInteger division ('\' instead of '/') because we don't want fractional hours.
The last step of the process is putting the values together in a string, using the Formatfunction to get the right number of digits in each section.
Function FormatTime(TimeElapsed As Integer) As String |
Let's take a closer look at this. First, I used the Mod operationto get the seconds. "Mod" finds the remainder when one number is divided byanother. In this case, when we divide our time elapsed by 60 (one minute), ourremainder is the number of seconds left over.
Then I did a similar process to find the minutes, only this time I had to divide the numberof seconds by 60 to get the total number of minutes. Again, I used the Modoperator to find the remainder (in this case, the number of leftover minutes).
Finally, the number of hours is the number of seconds divided by 3600. Once again, I usedInteger division ('\' instead of '/') because we don't want fractional hours.
The last step of the process is putting the values together in a string, using the Formatfunction to get the right number of digits in each section.
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
Programming
. Follow any responses to this post through RSS. You can leave a response, or trackback from your own site.
Subscribe to:
Post Comments (Atom)
Post a Comment