Start learning free

LESSON 21 OF 338 · CALCULATIONS WITH NUMBERS

Convert Hours and Minutes into Seconds in Go

Lesson Read the full explanation

We created two variables to store the number of hours and minutes we worked on a project. We want to convert this amount of time into seconds.

Use these two variables and some arithmetic operations to convert and print to the console the number of seconds the hours and minutes add up to.


Example:

For example, if hours was 1 and minutes was 2, the answer would be 3720.

Why? Because one hour and two minutes add up to 3720 seconds.


Need a hint?


1 hour is made up of 60 minutes. To how many minutes do hours and minutes add up?

hours and minutes add up to hours * 60 + minutes minutes.

Now, how many seconds are there in hours * 60 + minutes minutes?



As 1 minute is made up of 60 seconds, and we have (hours * 60 + minutes) minutes, the answer is: (hours * 60 + minutes) * 60


STEP 1 OF 1
    Ctrl + Enter to check

    GO · CALCULATIONS WITH NUMBERS

    Convert Hours And Mins Into Seconds in Go, explained

    The task

    Two variables hold the time spent on a project: hours and minutes. The program must print the whole duration in seconds.

    In the example, 1 hour and 2 minutes make 3720 seconds. The lesson's variables hold 13 hours and 47 minutes.

    Thinking in units

    Unit conversions are easiest in steps. You know two facts: one hour is 60 minutes, and one minute is 60 seconds. So:

    1. Turn the hours into minutes: hours * 60.
    2. Add the minutes you already have: hours * 60 + minutes. Now the whole duration is in a single unit, minutes.
    3. Turn that total into seconds: multiply by 60 again.

    With the example, 1 hour and 2 minutes: 1 * 60 + 2 is 62 minutes, and 62 times 60 is 3720 seconds. The conversion is correct because the hours and the minutes were first combined into one unit.

    Why parentheses are needed

    The second multiplication must apply to the whole sum, not only to minutes. Since * ranks above +, writing the expression without parentheses gives a different calculation:

    hours := 1
    minutes := 2
    
    fmt.Println((hours * 60 + minutes) * 60)  // 3720
    fmt.Println(hours * 60 + minutes * 60)    // 180
    

    The second line multiplies each part on its own and adds the results, which gives 60 + 120 = 180: the hours were converted to minutes but never to seconds. Parentheses make the addition happen first.

    There is another correct way to write the same thing, which does not need parentheses: hours * 3600 + minutes * 60. An hour has 3600 seconds, and a minute has 60. For 1 hour and 2 minutes that is 3600 + 120 = 3720, the same answer. It works because multiplication distributes over addition.

    The lesson's exercise

    The starting code declares the two values as integers:

    1. Declare the variables. var hours int = 13 and var minutes int = 47 state the type int explicitly. It is the same as hours := 13, where Go infers int from the literal.
    2. Build the total in minutes with hours * 60 + minutes. That is 13 * 60 + 47 = 827.
    3. Wrap it in parentheses and multiply by 60. The result is 827 * 60 = 49620.
    4. Print it with fmt.Println.
    var hours int = 13
    var minutes int = 47
    
    fmt.Println((hours * 60 + minutes) * 60)
    

    The output is 49620. A quick sanity check: 13 hours is 46800 seconds, 47 minutes is 2820 seconds, and together they make 49620.

    Why int is a good choice here

    All the values are whole numbers, and multiplication and addition of integers are exact, so no precision is lost. Go's int is 64 bits wide on this platform, which is far more than any realistic number of seconds needs, so overflow is not a concern for values like these.

    If you ever need the result in larger units again, the same idea runs backwards: divide by 60 for minutes, and use % to get the seconds left over.

    Common mistakes and gotchas

    • Missing parentheses. hours * 60 + minutes * 60 prints 3600 for the lesson's values instead of 49620, because the hours are only converted to minutes. Check the expression against the statement's example, where 1 and 2 must give 3720.
    • Mixing units. Adding hours and minutes directly, as in hours + minutes, adds numbers that measure different things.
    • Using 100 instead of 60. Time uses base 60, not base 10.
    • Unused variables. If you declare a variable in your own program and never read it, the compiler refuses to build. Here both are used in the print expression.
    • Forgetting to print. An expression on its own line does nothing; Go also rejects a bare expression statement such as hours * 60. Pass it to fmt.Println.

    Key takeaways

    • Convert in steps: hours to minutes, add minutes, minutes to seconds.
    • Parentheses make a sum run before a multiplication.
    • hours * 3600 + minutes * 60 is an equivalent form.
    • Integer arithmetic with whole numbers is exact, so int suits this task.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free