I have timestamps in database formatted like this: 2022-05-03 10:20:30.687 0200
.
I'm trying to convert that to a format that would allow me to compare two timestamps.
But, when I try to print that with
print(datetime.fromtimestamp(last_timestamp, pytz.utc))
I get the following error:
E TypeError: 'datetime.datetime' object cannot be interpreted as an integer
How can I get this to work?
Edit:
It appears I was overthinking this: comparing the strings directly works as expected.
CodePudding user response:
time_stamp1=str('2022-05-03 10:20:30.687 0200')
time_stamp2=str('2022-05-04 11:21:35.687 0200')
t1 = datetime.strptime(time_stamp1, "%Y-%m-%d %H:%M:%S.%f %z")
t2 = datetime.strptime(time_stamp2, "%Y-%m-%d %H:%M:%S.%f %z")
diff = t1 - t2
check datetime python for more
CodePudding user response:
As you can read from the documentation, the datetime.fromtimestamp
function converts a date in posix format (a 32bit integer) to a local date format.
Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.
If your date is in string format, use the function datetime.strptime(date_time_str, format)