Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert HH:MM:SS to seconds
#1
Hi,
I was doing this exercise that calculate totalseconds from user input.
This is text:
Quote:Write the algorithm that, having received a time input through three of its three components (hours,
minutes and seconds), it calculates the total value in seconds.
and this is my code:
from datetime import datetime

input_hour = str(input("Insert hour:minutes:seconds: "))
try:
    hour_start = datetime.strptime(input_hour, '%H:%M:%S')
    h, m ,s = hour_start.split(':')
    totalSeconds = int(h)*3600 + int(m) * 60 + int(s) 
    print(totalSeconds)
except ValueError:
    print("Wrong value!!!")
Return this error:
Error:
Attribute Error: 'datetime.datetime object has no attribute spilt'
Can you help me? I searched the internet but found nothing that is the same as my problem.
Reply
#2
In line 6, the datetime object does not have a split method.
But a datetime object has hour, minute, second, microsecond.
The use of datetime is a little bit wrong. It's used for dates/datetimes with or without timezones.
In addition you can have values like this:

hours = 55
minutes = 100
seconds = 1000
If you try to instantiate a datetime object with this values, you'll get a ValueError.

input_hour = input("Insert hour:minutes:seconds: ") # it's already a string
# if you still use Python 2.7, use raw_input or better use modern Python
try:
    h, m ,s = input_hour.split(':')

    # trick 33
    # h, m, s = map(int, input_hour.split(':'))
    # use it only if you understand what map does.

    totalSeconds = int(h) * 3600 + int(m) * 60 + int(s)
    print(totalSeconds)
except ValueError:
    print("Wrong value!!!")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Hi,
it works,but the map () method is used for iterators, while here I have a string, how can that work (sorry for the stupid question, but it was to understand).
Reply
#4
You don´t understand because you didn´t look at what input_hour.split(':') returns.

If you want to use datetime you need to read here: https://docs.python.org/3/library/datetime.html
Reply
#5
(Nov-26-2019, 01:17 PM)RavCOder Wrote: Hi,
it works,but the map () method is used for iterators, while here I have a string, how can that work (sorry for the stupid question, but it was to understand).

The method str.split returns a list, which is a sequence, which is iterable.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with module time and leap seconds Pedroski55 3 1,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Store variable data and display sum after 60 seconds the_dude 11 3,367 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,516 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Need to add hours min and seconds tester_V 5 3,031 Jun-02-2020, 05:29 PM
Last Post: tester_V
  Can't substract seconds from time Raj_Kumar 1 1,766 Apr-15-2020, 02:47 AM
Last Post: bowlofred
  How to calculate time in seconds rajeshE 1 2,079 Feb-15-2020, 11:07 PM
Last Post: Larz60+
  how to stop and start a script for 30 seconds laspaul 9 7,513 Jan-16-2020, 02:13 PM
Last Post: laspaul
  pyjama - frame every x seconds? MuntyScruntfundle 2 2,420 Jan-18-2019, 05:18 PM
Last Post: MuntyScruntfundle
  Conversion of Time Duration in seconds in python jdevansh99 0 2,829 Jun-05-2018, 05:12 PM
Last Post: jdevansh99
  starting a process every 10 seconds Skaperen 7 15,106 Dec-31-2017, 03:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020