Python Forum

Full Version: Trying to subtract datetime, getting error: TypeError: unsupported operand type(s) fo
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
Because you're passing gfsDate, not GFSDatetime. I'm assuming gfsDate came from the time module, which uses tuples.