Dominic M.
Dominic M.

Reputation: 913

Django Datetime.strptime displays unwanted time

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

Answers (2)

Daniel Roseman
Daniel Roseman

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

Pedro Mendes
Pedro Mendes

Reputation: 66

data = datetime.strptime("2019-05-26", '%Y-%m-%d')
print(data)

Upvotes: 0

Related Questions