0

I have a timestamp that was created with datetime module and now I need to convert '2020-10-08T14:52:49.387077+00:00' into 08/OCT/2020?

Can datetime do this? I have tried strptime but I think the +00:00 at the end is causing errors.

kravb
  • 417
  • 4
  • 16
  • Does this answer your question? [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – msanford Oct 08 '20 at 22:35
  • @msanford I tried with strptime as in the link but it didn't work. As per the answer below, splitting the timestamp string along the "T" would have allowed strptime to work, but I didn't think of doing that. – kravb Oct 09 '20 at 14:44

1 Answers1

1

Use fromisoformat and strftime method from datetime package in Python 3.7:

from datetime import datetime
time = '2020-10-08T14:52:49.387077+00:00'
new_time = datetime.fromisoformat(time).strftime("%d/%b/%Y")
print(new_time)

Or with strptime:

from datetime import datetime
time = '2020-10-08T14:52:49.387077+00:00'
new_time = datetime.strptime(time.split('T')[0], "%Y-%m-%d").strftime("%d/%b/%Y")
print(new_time)

Output:

08/Oct/2020
Grayrigel
  • 3,474
  • 5
  • 14
  • 32