Start learning free

LESSON 23 OF 348 · CALCULATIONS WITH NUMBERS

Convert Hours and Minutes into Seconds in TypeScript

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

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Convert Hours And Mins Into Seconds in TypeScript, explained

    Problem overview

    The starter code stores how long someone worked on a project, as a number of hours and a number of minutes. The task is to print the total time in seconds.

    let hours: number = 13;
    let minutes: number = 47;
    

    The example in the task uses 1 hour and 2 minutes. That is 62 minutes, and since a minute has 60 seconds, it is 3720 seconds. The goal is an expression that does this for any values of hours and minutes.

    Thinking in units

    Converting between units is a chain of multiplications. The easiest way to avoid mistakes is to convert one unit at a time and keep track of what each intermediate number measures.

    1. Hours to minutes. One hour is 60 minutes, so hours * 60 is the number of minutes in the hours part. For 13 hours that is 780 minutes.
    2. Add the minutes. Both parts are now in minutes, so they can be added: hours * 60 + minutes. That gives 827 minutes. You can only add two quantities when they have the same unit.
    3. Minutes to seconds. One minute is 60 seconds, so the total number of minutes is multiplied by 60.

    Step 3 needs the whole sum from step 2, so the sum goes in parentheses:

    let hours: number = 13;
    let minutes: number = 47;
    
    console.log((hours * 60 + minutes) * 60);
    

    It prints:

    49620
    

    Check by hand: 827 minutes times 60 is 49620 seconds.

    Why the parentheses matter

    Multiplication runs before addition. Without the parentheses, hours * 60 + minutes * 60 would be read as (hours * 60) + (minutes * 60). That converts only the minutes to seconds and leaves the hours in minutes, mixing two units. For the example of 1 hour and 2 minutes, the mistaken expression gives 180 instead of 3720.

    The outer expression is a product of a sum and 60, and the only way to say "add first, then multiply" is with parentheses. Another correct form multiplies each part by its own factor, converting hours straight to seconds with 3600:

    console.log(hours * 3600 + minutes * 60);   // 49620
    

    Both forms give the same result. The lesson's form builds the answer from the 60-to-1 relationship twice, which is easier to remember than the number 3600.

    Notes about numbers

    • All the values are number. The type has no separate integer form, so the code would also work with fractional input, such as 1.5 hours. The result may then show decimals.
    • Nothing overflows. The result of an exercise like this is far below the limit where whole numbers stop being exact, which is about 9 quadrillion.
    • Name the pieces if it helps. You may store the intermediate value in a variable, such as const totalMinutes: number = hours * 60 + minutes;, and then print totalMinutes * 60. The result is the same, and the code explains itself.

    Common mistakes

    • Missing parentheses. hours * 60 + minutes * 60 mixes units, as shown above.
    • Multiplying by 60 only once. hours * 60 + minutes is 827, the number of minutes. The task asks for seconds.
    • Using 100 instead of 60. Time units are not decimal. An hour has 60 minutes, not 100.
    • Printing a string. Writing the expression inside quotes prints the text of the expression, not its value.
    • Typing 13 and 47 into the expression. Use hours and minutes, so the answer follows the variables.

    Testing

    • hours = 1, minutes = 2 gives 3720.
    • hours = 0, minutes = 5 gives 300.
    • hours = 2, minutes = 0 gives 7200.
    • hours = 13, minutes = 47 gives 49620.

    Key takeaways

    • Convert one unit at a time, and add only quantities that share a unit.
    • Parentheses make an addition run before the multiplication that follows it.
    • Check your expression against the task's example by hand.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free