C++ || Convert Time From Seconds Into Hours, Min, Sec Format

Print Friendly, PDF & Email

Here is another simple programming assignment. This page will demonstrate how to convert time from -seconds- into HH::MM::SS (hours, minutes seconds) format. So for example, if you had an input time of 9630 seconds, the program would display the converted time of 2 hours, 40 minutes, and 30 seconds.

Using simple math, this program utilizes the modulus operator, and the division operator during the conversion process.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Modulus - What is it?
How Many Seconds Are In One Hour?
How Many Seconds Are In One Minute?

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compiled five separate times to display different output)

====== RUN 1 ======

Enter a time in seconds: 9630

The time in HH:MM:SS format is: 2 hours, 40 minutes, and 30 seconds!

====== RUN 2 ======

Enter a time in seconds: 7200

The time in HH:MM:SS format is: 2 hours, 0 minutes, and 0 seconds!

====== RUN 3 ======

Enter a time in seconds: 45

The time in HH:MM:SS format is: 0 hours, 0 minutes, and 45 seconds!

====== RUN 4 ======

Enter a time in seconds: 134

The time in HH:MM:SS format is: 0 hours, 2 minutes, and 14 seconds!

====== RUN 5 ======

Enter a time in seconds: 31536000

The time in HH:MM:SS format is: 8760 hours, 0 minutes, and 0 seconds!

Was this article helpful?
👍 YesNo

5 Responses to C++ || Convert Time From Seconds Into Hours, Min, Sec Format

  1. Avatar Varis Ali says:

    Not run this your program

  2. Avatar Ritesh says:

    Thank you for the code. 🙂

  3. Avatar dagimawit siyum says:

    Write a program that reads in a time in seconds, and computes how many hours and minutes it contains. Thus, 3700 should yield: 1 hour, 1 minute, and 40 seconds. (Hint: use the mod function)

  4. Avatar asma qamar says:

    this program is so good i completely understand its code .thank you so much

  5. Avatar sasasa says:

    what does mean that % sight? What he do?

Leave a Reply