Python Forum
Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo
#1
Hi there

From my understanding, you can use the
import datetime
module to subtract two different datetimes. But when I try to execute this, my code throws the error: TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'tuple'. Below is my code. Any reason why this isn't working? Everything is formatted the same.

flightDate = datetime.datetime(flight_year, flight_month, flight_day, flight_hour, 0, 0) 
GFSDate = int(time.strftime("%Y%m%d"))
GFSDate = str(GFSDate)
GFSDateTime = datetime.datetime(int(GFSDate[:4]),int(GFSDate[4:6]),int(GFSDate[6:]),GFSTime, 0, 0)
def findGFSTimeIndex(predictionDateTime, GFSDateTime):
    diff = predictionDateTime - GFSDateTime
    return int(np.round((diff.total_seconds()/3600)/3))
But I am actually using it with these parameters later on, which is where it is getting snagged:
timeIndex = GFSReader.findGFSTimeIndex(flightDate, gfsDate)
Which then gives the error:
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'tuple'
I'm confused where this is coming from because everywhere I have looked, it says you can subtract datetime.datetime values. Any suggestions? Thanks!
Reply
#2
Because you're passing gfsDate, not GFSDatetime. I'm assuming gfsDate came from the time module, which uses tuples.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 502 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  subtract 2 datetime string jss 4 771 Oct-19-2023, 02:42 PM
Last Post: Larz60+
  Wrong type error rowan_bradley 6 1,214 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,170 Jul-22-2023, 11:33 PM
Last Post: jhancock
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,231 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,935 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,300 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,141 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 32,835 May-07-2022, 08:07 AM
Last Post: menator01
  Python Anytree - Is not of type 'NodeMixin' error georgebijum 3 2,083 May-05-2022, 01:43 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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