{"id":7907,"date":"2023-11-02T07:47:24","date_gmt":"2023-11-02T02:17:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=7907"},"modified":"2023-11-10T12:12:51","modified_gmt":"2023-11-10T06:42:51","slug":"python-program-to-convert-seconds-into-day-hours-minutes-and-seconds","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-seconds-into-day-hours-minutes-and-seconds\/","title":{"rendered":"Python program to convert seconds into day, hours, minutes and seconds"},"content":{"rendered":"
In this post, we will look at various Python techniques for converting seconds to hours and minutes.<\/p>\n
Date and Time<\/strong><\/p>\n Python, as a multipurpose language, can be used for a variety of reasons. Python includes a number of packages that help us with data manipulation tasks.<\/p>\n To achieve the same result in timestamp conversion, i.e. conversion of seconds to hours or minutes, numerous strategies might be considered.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to convert seconds into hours and minutes in python some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n We will write a generic function which converts the given time into hours and minutes<\/p>\n First, we convert the supplied seconds value to the 24 hour format.<\/p>\n Furthermore, because one hour is equal to 3600 seconds and one minute is equal to 60 seconds, we use the below mathematical logic to convert seconds to hours and minutes.<\/p>\n timehour = timeseconds\/3600<\/p>\n timeminutes = timeseconds\/\/60<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Python time module includes time. By giving the format codes to the strftime()<\/strong> function, you can display the timestamp as a string in a certain format.<\/p>\n The function time.gmtime()<\/strong> is used to convert the value supplied to it into seconds. Furthermore, the time.strftime()<\/strong> function converts the value supplied from time.gmtime()<\/strong> into hours and minutes using the format codes provided.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Python datetime module has a number of built-in functions for manipulating dates and times. The date and time. The timedelta() function manipulates and represents data in the correct time format.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n You may get the result quickly with only two mathematical operations by using the divmod() function, which performs a single division to generate both the quotient and the remainder.<\/p>\n This is similar to method 1<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In this post, we will look at various Python techniques for converting seconds to hours and minutes. Date and Time Python, as a multipurpose language, can be used for a variety of reasons. Python includes a number of packages that help us with data manipulation tasks. To achieve the same result in timestamp conversion, i.e. …<\/p>\ntimeSeconds=30000<\/pre>\n
converting given time in seconds 30000 = 8 hrs 20 minutes<\/pre>\n
Different Ways to convert seconds into Hours and Minutes in Python<\/h2>\n
\n
Method #1: Using mathematical formula<\/h3>\n
timeseconds = timeseconds % (24*3600)<\/code><\/p>\n
def convertSec(timeseconds):\r\n secondValue = timeseconds % (24 * 3600)\r\n timeHour = secondValue \/\/ 3600\r\n secondValue %= 3600\r\n timeMinutes = secondValue \/\/ 60\r\n secondValue %= 60\r\n print(\"converting given time in seconds\",\r\n timeseconds, \"=\", timeHour, \"hrs\", timeMinutes, \"minutes\")\r\n\r\n\r\n# given time in seconds\r\ntimeseconds = 30000\r\n# passing given time in seconds to convertSec function to convert it to minutes and hours\r\nconvertSec(timeseconds)\r\n<\/pre>\n
converting given time in seconds 30000 = 8 hrs 20 minutes<\/pre>\n
Method #2: Using time module in python<\/h3>\n
import time\r\ngivenseconds = 30000\r\n#converting\u00a0given\u00a0time\u00a0in\u00a0seconds\u00a0to\u00a0hours\u00a0and\u00a0minutes\r\ntemp = time.gmtime(givenseconds)\r\nresultantTime = time.strftime(\"%H:%M:%S\", temp)\r\n#printing the given time in seconds to hours and minutes\r\nprint(\"converting given time in seconds\", givenseconds,\r\n \"=\", resultantTime)\r\n<\/pre>\n
converting given time in seconds 30000 = 08:20:00<\/pre>\n
Method #3: Using datetime module in Python<\/h3>\n
import datetime\r\n# given time in seconds\r\ngivenSeconds = 30000\r\n#converting given time in seconds to hours and minutes\r\nresultantTime = datetime.timedelta(seconds=givenSeconds)\r\n# printing the given time in seconds to hours and minutes\r\nprint(\"converting given time in seconds\", givenSeconds,\r\n \"=\", resultantTime)\r\n<\/pre>\n
converting given time in seconds 30000 = 8:20:00<\/pre>\n
Method #4:Using divmod function<\/h3>\n
# given time in seconds\r\ntimeSeconds = 30000\r\n# storing it in another variable\r\ngivenSeconds = timeSeconds\r\n# converting it into minutes and seconds\r\ntimeMinutes, timeSeconds = divmod(timeSeconds, 60)\r\n# converting it into hours and minutes\r\ntimeHour, timeMinutes = divmod(timeMinutes, 60)\r\n# printing the given time in seconds to hours and minutes\r\nprint(\"converting given time in seconds\", givenSeconds,\r\n \"=\", timeHour, \"hrs\", timeMinutes, \"minutes\")\r\n<\/pre>\n
converting given time in seconds 30000 = 8 hrs 20 minutes<\/pre>\n
\n