Search:
Home Math Games Word Games Strategy Reference Junior Games Daily Puzzles Printables Problems Members

Home Reference Programming Tips

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.
Index Previous    Next    Teachers    About   

Seconds, Minutes, and Hours

I suspect every young computer programmer has dreamed, at one time or another, about creating the next amazing computer game. The fact is, it's a dream that is realized by a very very small percentage of programmers. But we all have to give it a shot, right? One of the common things you'll need to do is to take an elapsed time in seconds and put it into a format that your game player can read. After all, the player doesn't want to see that he took 12752 seconds to complete the level, right? He wants to know that it took him 3 hours, 32 minutes, and 32 seconds.

Now, how did I come up with that? Easy. I have a little function that converts seconds into other units. Let's take a look at it.

Function FormatTime(TimeElapsed As Integer) As String
   Dim Seconds As Integer
   Dim Minutes As Integer
   Dim Hours As Integer
   
   'Find The Seconds
   Seconds = TimeElapsed Mod 60
   
   'Find The Minutes
   Minutes = (TimeElapsed \ 60) Mod 60
   
   'Find The Hours
   Hours = (TimeElapsed \ 3600)
   
   'Format The Time
   If Hours > 0 Then
      FormatTime = Format(Hours,"00") & ":"
   End If
   FormatTime = Format(Hours, "00")  & ":"
   FormatTime = FormatTime & Format(Minutes, "00") & ":" 
   FormatTime = FormatTime & Format(Seconds, "00")
End Function

Let's take a closer look at this. First, I used the Mod operation to get the seconds. "Mod" finds the remainder when one number is divided by another. In this case, when we divide our time elapsed by 60 (one minute), our remainder 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 number of seconds by 60 to get the total number of minutes. Again, I used the Mod operator 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 used Integer 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 Format function to get the right number of digits in each section.

Of course, there are probably other ways you might want to format your time, and that's what the Other Scenarios section of the page explores.

Other Scenarios

  1. How would your function change if you wanted to display hours only if the time was greater than sixty minutes?
  2. How would your function change if you wanted to display minutes only if the time was greater than sixty seconds?
  3. Rewrite the function to handle the number of days, in addition to minutes, seconds, and hours.
  4. Write a function that does the opposite of the function shown here. The function receives three variables: Hours, Minutes, and Seconds, and it returns the total number of seconds.

Index Previous    Next    Teachers    About   

"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 



 
Search For More Resources Search For More Educational Resources

 

Top Games On This Site

 
Adders!
Scramble
Entrapment
Secret Word
Trio Match
 

Just a few of the educational game resources available on The Problem Site!

 
 






Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page