Reputation: 181
I need to compare my input time with current time. If my input time is older than 5 days, I will do d = X_value
. Otherwise, d = Y_value
. The format of my input time like this:
my_input__time: 2017-07-05 22:52:00+00:00
my view.py looks like this:
import datetime
five_days_ago = datetime.datetime.now() - datetime.timedelta(5)
if my_input_time < five_days_ago:
d = X_value
else:
d = Y_value
I am getting TypeError:can't compare datetime.datetime to unicode. How do I convert my_input_tine
and five_dys_ago
into same format so I can compare?
Upvotes: 0
Views: 5071
Reputation: 4213
Try with this and tell me
import datetime
five_days_ago = datetime.datetime.now() - datetime.timedelta(5)
my_input_time = datetime.datetime.strptime(my_input__time, '%Y-%m-%d %H:%M:%S+00:00')
if my_input_time < five_days_ago:
d = X_value
else:
d = Y_value
Upvotes: 1
Reputation: 116
datetime.now() will return the date down to seconds.
So to add to @thaavik 's answer you could do something like this to compare more accurately:
datetime.strptime(my_input_time, '%Y%m%d %h:%m:%s')
Upvotes: 1
Reputation: 3317
from datetime import datetime
t = datetime.strptime(my_input_time, '%Y%m%d') # or whatever your datetime format is
you can also make setting d
more concise:
d = X_value if t < five_days_ago else Y_value
Upvotes: 0