-1
import datetime
a = int(input("sekund1"))
b = int(input("sekund2"))
time1 = datetime.strptime(a,"%H%M") 
time2 = datetime.strptime(b,"%H%M") 
diff = time1 -time2
diff.total_seconds()/3600

I am trying to create a "calculator" for given times in python, my knowledge in the language is very limited and this is how far I've managed to come.

Although this is very limited as it will only subtract given seconds, I am trying to subtract 2 given times in the format HH:MM:SS, but this is where I come at a stop.

I get an error

module 'datetime' has no attribute 'strptime'

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
im very stupid
  • 31
  • 1
  • 1
  • 4
  • What's wrong with this code? It looks like it already does what you have described. – mkrieger1 Sep 14 '22 at 20:43
  • When typing in 2 given seconds I get an AttributeError "module 'datetime' has no attribute 'strptime'" – im very stupid Sep 14 '22 at 20:45
  • I also am trying to subtract 2 times in the format HH:MM:SS as stated in the post but all attempts gives me some sort of error – im very stupid Sep 14 '22 at 20:46
  • @imverystupid it should be `datetime.datetime.strptime` not `datetime.strptime` but you will get another error when you fix that. – It_is_Chris Sep 14 '22 at 20:46
  • Does this answer your question? [AttributeError: 'datetime' module has no attribute 'strptime'](https://stackoverflow.com/questions/19480028/attributeerror-datetime-module-has-no-attribute-strptime) – mkrieger1 Sep 14 '22 at 20:48
  • @It_is_Chris Why is it datetime.datetime.strptime? never knew this, but yes I still get a TypeError when entering the 2 variables telling me that the value must be a string and not an integer? – im very stupid Sep 14 '22 at 20:48
  • @mkrieger1 yes although i'm met with a new error – im very stupid Sep 14 '22 at 20:49
  • @imverystupid datetime is the package name and the class name so you can either do `from datetime import datetime` and then do `datetime.strptime` or if you just import the package like you are doing now and do `datetime.datetime.strptime` – It_is_Chris Sep 14 '22 at 20:50
  • Okay then you could ask a new question about that other error. – mkrieger1 Sep 14 '22 at 20:51
  • @It_is_Chris ah, thanks for that, I didn't know that. But how can I in that case subtract 2 times that the user inputs in a HH:MM:SS format? – im very stupid Sep 14 '22 at 20:51
  • @mkrieger1 is that necessary? don't want to fill up the board with questions after just asking one – im very stupid Sep 14 '22 at 20:52
  • @imverystupid [see this answer](https://stackoverflow.com/a/151212/9177877) – It_is_Chris Sep 14 '22 at 20:53
  • Of course you should first try to resolve the problem yourself. – mkrieger1 Sep 14 '22 at 20:53
  • @It_is_Chris Unfortunately, that doesn't answer my question. You see, the dates are already defined in the code while im trying to subtract 2 times from a given value by the user. – im very stupid Sep 14 '22 at 20:56
  • 1
    @imverystupid it works the same way just copy that code and replace the `a` and `b` variables with `a = input("sekund1")` and enter the date as in the example and it will work. Just make sure you enter the date in the correct format. – It_is_Chris Sep 14 '22 at 20:59

2 Answers2

1

I tried out your code and found a few tweaks that needed to be made. Following is a code snippet with those tweaks.

from datetime import datetime     # To make "datetime.strptime()" work
a = str(input("sekund1 "))
b = str(input("sekund2 "))
time1 = datetime.strptime(a,"%H:%M")    # Was missing the ":" symbol
time2 = datetime.strptime(b,"%H:%M")    # Was missing the ":" symbol
diff = time1 -time2

print("Difference in hours:", diff.total_seconds()/3600)

Here was a sample of output.

@Una:~/Python_Programs/Seconds$ python3 Diff.py 
sekund1 12:45
sekund2 10:45
Difference in hours: 2.0

Give that a try.

NoDakker
  • 3,390
  • 1
  • 10
  • 11
1

I am not sure I understand what you want to do but here my interpretation of it, in the simplest way I could think of.

import datetime

a = input("a [HH:MM:SS] : ")
b = input("b [HH:MM:SS] : ")

a_from_str_to_list = a.split(':')
b_from_str_to_list = b.split(':')

a_seconds = ( int(a_from_str_to_list[0]) * 3600 ) + ( int(a_from_str_to_list[1]) * 60 ) + int(a_from_str_to_list[2])

b_seconds = ( int(b_from_str_to_list[0]) * 3600 ) + ( int(b_from_str_to_list[1]) * 60 ) + int(b_from_str_to_list[2])

c_secondds = a_seconds - b_seconds

t = str(datetime.timedelta(seconds = c_secondds))

print(t)

given 01:01:13 and 00:00:13 as input it would print 01:01:00