We can create a Show
Nội dung chính
This can all be done in one line by
and some tests to show it works:
Note how the Yes, to answer
works just the same as above. In this tutorial, we will be talking about time. Don’t worry, this isn’t a boring history tutorial, rather we will be looking at different ways of converting time in seconds to time in hours, minutes, and seconds. Moving forwards we will be referring to time in hours, minutes and seconds as time in the preferred format. It will look like :
Let’s take some ‘time’ and think about the problem at hand. No doubt python has amazing modules to do the conversion for us. But let’s try and write our own program first before we move on to the in-built modules. Building a Custom function to convert time into hours minutes and secondsTo write our own conversion function we first need to think about the problem mathematically. How do you convert seconds into the preferred format? You need to get the value of hours, minutes and seconds. We are going to assume that the time in seconds doesn’t exceed the total number of seconds in a day. If it does we will divide it with the total number of seconds in a day and take the remainder. This is mathematically represented as :
% operator gives the remainder. 24*3600 since one hour has 3600 seconds (60*60) and one day has 24 hours. After this we can proceed and calculate the hour value from seconds. 1. Get the hour valueTo get the hour value from seconds we will be using the floor division operator (//). It returns the integral part of the quotient. Since we need the number of hours, we will divide the total number of seconds (n) by the total number of seconds in an hour (3600). Mathematically this is represented as :
After this we need to calculate the minutes. 2. Get the minute valueTo calculate the value of minutes we need to first divide the total number of seconds by 3600 and take the remainder. Mathematically this is represented as :
Now to calculate the value of minutes from the above result we will again use the floor operator.
A minute has sixty seconds hence we floor the seconds value with 60. After calculating minute value we can move forward to calculate the seconds’ value for our preferred format. 3. Get the seconds valueTo get seconds value we again need to divide the total number of seconds by the number of seconds in one minute (60) and take the remainder. Mathematically this is done as follows :
This will give the second value that we need for our preferred format. 4. Complete codeLet’s compile the above knowledge in a python function.
Output :
Using the Time moduleNow let’s look at an inbuilt module that lets us convert seconds into our preferred format in one line of code. The time module defines the epoch as January 1, 1970, 00:00:00 (UTC) in Unix systems (system dependent). Epoch is basically the start of time for a computer. Think of it as day 0. Whenever we convert seconds using the time module, this epoch is used as the reference point. To output the epoch in your system, use the following line of code :
To convert seconds into preferred format use the following line of code:
This line takes the time in seconds as ‘n’ and then lets you output hour, minute, and second value separately. The complete python code is as follows:
Output :
The time module also gives you the option to display some extra information such as day, month, and year.
Let’s try using %a and %b.
Output :
Using Datetime moduleYou can also use the timedelta method under the DateTime module to convert seconds into the preferred format. It displays the time as days, hours, minutes, and seconds elapsed since the epoch. The python code to convert seconds into the preferred format using Datetime module is as follows:
Output :
ConclusionThis tutorial looked at three different ways you can use to convert seconds into hours, minutes, and seconds. Broadly there are two different ways to go about the problem. Either you write your own function or use an inbuilt module. We started by writing our own function and then looked at the time and DateTime module. How do you convert input to time in Python?time = input("Enter time in HH:MM\n") hour, min = [int(i) for i in time. split(":")] print("It is", hour, "hours and", min, "minutes") # time. split(":") splits the time into two parts, HH and MM. How do you convert int to datetime in Python?Use pandas. to_datetime() to Convert Integer to Date & Time Format. Let's suppose that your integers contain both the date and time. In that case, the format should be specify is '%Y%m%d%H%M%S' . How do you convert int to hours?How to convert decimal minutes to time format. 78.6 minutes can be converted to hours by dividing 78.6 minutes / 60 minutes/hour = 1.31 hours.. 1.31 hours can be broken down to 1 hour plus 0.31 hours - 1 hour.. 0.31 hours * 60 minutes/hour = 18.6 minutes - 18 minutes.. 0.6 minutes * 60 seconds/minute = 36 seconds - 36 seconds.. How do I get hours and minutes in Python?To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds. |