Home        Math Games        Word Games        Daily        Reference        Miscellaneous        Junior!        Problems



Beginner Programming Tips

Beginner Programming Tips and Tricks

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   

AddThis Social Bookmark Button     Link To This Page

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   

AddThis Social Bookmark Button     Link To This Page
"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.

 


Want To Try Something Completely Different?

The Treasure Hunt

Pirate's Map

Easter Eggs



Site Features

Word Games

Word Games

Math Games

Math Games

Daily Puzzles

Daily Puzzles

Brainfood

Brainfood

Math Problems

Problems

Miscellaneous

Miscellaneous


 
Search For More Resources

Search For More Educational Resources

Find more educational, problem solving, and puzzle resources using the Google safe-search below.

Google

Member Features
Login
 

Picture Word
Four-Scramble
Word Search
Blackberry Game
Telephone Game
Cheater Hangman
Word Grid
Secret Word
Scrambled Word
One of These
Hangman
 

Entrapment
Adders!
Side By Side
One To Ten
Sub Triangles
Magical Squares
Math Scramble
Secret Number
Secret Number 2
Fractional Hi Lo
Concentration
Monty Hall Game
 

Trio Match
Treasure Hunt
Pirate's Map
Fizziks Tilt
Zero Gravity
Easter Egg Hunt
Quad Puzzle
Tic Tac Toe
Rotating Block
 

Codes
Programming
Search It Out!
 
Secret Word
Scrambled Word
Secret Number
Word Grid
 
Brainfood
Math HS
Maine Page
Calculus Page
 
Contact
About
Related Sites
Link to TPS
 

Bookmarking and Linking
Bookmark/Link


Home        Math Games        Word Games        Daily        Reference        Miscellaneous        Junior!        Problems