Reputation: 913
I am trying to inject a date into my django html template.
I have the string "2019-05-26"
and I am trying to convert it to datetime format for django to display as "May 26, 2019"
on the webpage. The problem is that it displays "May 26, 2019 Midnight"
instead of just "May 26 2019"
. It adds the "Midnight"
because of a trailing 00:00 that is added on from the function, How do I remove the trailing "midnight"
?
timestamp = transaction["timestamp"]
data = (timestamp[:10]) if len(timestamp) > 10 else timestamp
print(data)
data2 = datetime.strptime(data, '%Y-%m-%d')
print("timestamp:", transaction["timestamp"])
timestamp: 2019-05-26T20:55:44.993368+00:00
Upvotes: 0
Views: 142
Reputation: 599480
datetime.strptime()
returns a datetime object. You just need the date, so call the date()
method.
data2 = datetime.strptime(data, '%Y-%m-%d').date()
Upvotes: 2