Convert hour minute second to seconds python
Basically I have the inverse of this problem: Python Time Seconds to h:m:s Show I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I need the integer number of seconds that it represents. How can I do this in python? For example:
etc
asked Jun 19, 2011 at 14:05
1
LeopardShark 2,8922 gold badges18 silver badges31 bronze badges answered Jun 19, 2011 at 14:11
taskinoortaskinoor 44.9k12 gold badges116 silver badges139 bronze badges 0
answered Jun 19, 2011 at 14:22
FMcFMc 41.2k13 gold badges76 silver badges131 bronze badges 4 Using datetime module
Output: 36930 answered May 14, 2014 at 22:52
kaushkaush 1611 silver badge7 bronze badges 3 Without many checks, and assuming it's either "SS" or "MM:SS" or "HH:MM:SS" (although not necessarily two digits per part):
This is a different “spelling” of FMc's answer :) answered Jun 19, 2011 at 14:46
tzottzot 89k29 gold badges136 silver badges200 bronze badges 2 Using
That's why to get the total of seconds is necessary to subtract the year, month and day.
answered May 19, 2020 at 11:52
1 You can use lambda and reduce a list and the fact that m=60s and h=60m. (see "Reducing a List" at http://www.python-course.eu/lambda.php)
answered Apr 8, 2017 at 15:53
You can split the time into a list and add each individual time component, multiplying the hours component by 3600 (the number of seconds in an hour) and the minutes component by 60 (number of seconds in a minute), like:
answered Aug 24, 2018 at 9:46
For %H:%M:%S.%f
answered Sep 9, 2021 at 9:40
answered Jun 19, 2011 at 14:10
DaClownDaClown 3,8916 gold badges30 silver badges30 bronze badges 1 I didn't really like any of the given answers, so I used the following:
JayRizzo 2,7843 gold badges31 silver badges42 bronze badges answered Aug 30, 2017 at 23:18
0 Expanding on @FMc's solution which embodies half of Horner's method. Advantage of Horner's method: Skip reversing the list, avoid power calculation.
or, if you dislike reduce (as does Guido van Rossum and @0xc0de below):
If you prefer zip (as does @nathan-rice below):
answered Aug 22, 2019 at 13:12
1 Another alternative if you have days on string:
answered Oct 15, 2014 at 23:47
ItaluxItalux 751 gold badge1 silver badge9 bronze badges 2 Just a simple generalization to the great response of taskinoor In the context of my problem the format is similar, but includes AM or PM. Format 'HH:MM:SS AM' or 'HH:MM:SS PM' For this case the function changes to:
answered Nov 16, 2020 at 20:18
In Pandas using @JayRizzo 's cool function and a list comprehension:
Sven Eberth 2,97312 gold badges22 silver badges27 bronze badges answered Jun 22, 2021 at 18:38
How do you convert hours minutes seconds to seconds in Python?Steps to convert hours, minutes, seconds to seconds:
Multiply hours by 3600. Multiple minutes by 60. Add all the numbers to the seconds variable.
How do you convert HH MM SS to HH in Python?“transform string to time HH:MM:SS python” Code Answer's. from datetime import datetime.. datetime_str = '09/19/18 13:55:26'. datetime_object = datetime. strptime(datetime_str, '%m/%d/%y %H:%M:%S'). print(type(datetime_object)). print(datetime_object) # printed in default format.. How do I calculate hours minutes and seconds in Python?Use the divmod() Function to Convert Seconds Into Hours, Minutes, and Seconds in Python. The divmod() function can convert seconds into hours, minutes, and seconds. The divmod() accepts two integers as parameters and returns a tuple containing the quotient and remainder of their division.
How do you convert HH MM SS to minutes in Python?Python Program to Convert time into minutes. def convert_time(hrs, min):. min= hrs*60+min.. return min.. h=int(input("Enter the hours:")). m=int(input("Enter the minutes:")). m=convert_time(h,m). print("Total Minutes=",m). |