-1

I'm using Python 3.10 and I'm trying to subtract two time values from each other. Now I have tried bunch of ways to do that but getting errors.

day_time = timezone.now()
day_name = day_time.strftime("%Y-%m-%d %H:%M:%S")
end_Time = datetime.strptime(latest_slots.end_hour, '%Y-%m-%d %H:%M:%S')
print(end_Time- day_name)

error: TypeError: unsupported operand type(s) for -: 'str' and 'str'

I also tried:

day_time = timezone.now()
end_Time = datetime.strptime(latest_slots.end_hour, '%Y-%m-%d %H:%M:%S')
print(end_Time- day_time)

error: TypeError: can't subtract offset-naive and offset-aware datetimes

And this as well:

day_time = timezone.now()
end_Time = datetime.strptime(latest_slots.end_hour, '%Y-%m-%d %H:%M:%S.000000.00+00')
print(end_Time- day_time)

error: ValueError: time data '2022-11-27 00:00:00' does not match format '%Y-%m-%d %H:%M:%S.000000.00+00'

wovano
  • 4,543
  • 5
  • 22
  • 49
Ahmed Yasin
  • 185
  • 8
  • Does this answer your question? [Python subtract time](https://stackoverflow.com/questions/24973163/python-subtract-time) – wovano Nov 18 '22 at 10:14
  • See also [Can't subtract offset-naive and offset-aware datetimes](https://stackoverflow.com/questions/796008/cant-subtract-offset-naive-and-offset-aware-datetimes) – wovano Nov 18 '22 at 10:14
  • [How to subtract time?](https://stackoverflow.com/questions/71042018/how-to-subtract-time) – wovano Nov 18 '22 at 10:15
  • [subtract two times in python](https://stackoverflow.com/questions/5259882/subtract-two-times-in-python) – wovano Nov 18 '22 at 10:15
  • [Subtract time from datetime.time object](https://stackoverflow.com/questions/42594164/subtract-time-from-datetime-time-object) – wovano Nov 18 '22 at 10:16
  • [Python: Subtracting time from datetime](https://stackoverflow.com/questions/46474350/python-subtracting-time-from-datetime) – wovano Nov 18 '22 at 10:16
  • and almost 3000 more questions and answers in [search](https://stackoverflow.com/search?q=%5Bpython%5D+subtract+time) – wovano Nov 18 '22 at 10:17

3 Answers3

0

Your second approach is almost right, but you should use

day_time = datetime.now()

When you subtract you will get a a datetime.timedelta object

I think the issue with your second approach is that by using timezone you also have the timezone as part of the datetime, instead of just the date and time.

Ftagliacarne
  • 675
  • 8
  • 16
0

You can solve this by using a combination of the datetime and timedelta libraries like this:

from datetime import datetime,timedelta
day_time = datetime.now()
end_time = datetime.now() + timedelta(days= 2,hours =7)
result = end_time - day_time
print(result)

# Output :--> 2 days, 7:00:00.000005
Nick
  • 3,454
  • 6
  • 33
  • 56
Bharat Adhikari
  • 334
  • 1
  • 6
0

I have solved this problem by doing the following approach.

        #first i get the current time
        day_time = timezone.now()
        #second converted that into end_time format
        day_time = day_time.strftime("%Y-%m-%d %H:%M:%S")
        #third converted that string again into time object
        day_time = datetime.strptime(day_time, '%Y-%m-%d %H:%M:%S')
        end_Time = datetime.strptime(latest_slots.end_hour, '%Y-%m-%d %H:%M:%S')
Ahmed Yasin
  • 185
  • 8