0

I use orator to store created_at timestamps in my database tables. When I retrieve this timestamp (job.created_at) it look like this. 2019-08-16T10:36:03.562908+00:00

I need to convert this into 16-08-2019

I have tried datetime.datetime

ts = str(job.created_at)
f = '%Y-%m-%d %H:%M:%S'
print(datetime.datetime.strptime(ts, f))

This is expecting %Y-%m-%d %H:%M:%S but is being passed 2019-08-16T10:36:03.562908+00:00 so I get a format does not match error.

I'd forgotten how tricky handling dates in Python is. Is there an equivalent to php's Carbon package?

Any help would be gratefully appreciated.

Finchy70
  • 361
  • 4
  • 22

1 Answers1

1

If you only need to convert the string, try just slicing the data into the format you want:

unformatted_date = '2019-08-16T10:36:03.562908+00:00'
formatted_date = unformatted_date[8:10] + '-' + unformatted_date[5:7] + '-' + unformatted_date[:4]

If you want to use a datetime:

import datetime

unformatted_date = '2019-08-16T10:36:03.562908+00:00'
my_date = datetime.datetime.strptime('2019-01-04T16:41:24+0200', "%Y-%m-%dT%H:%M:%S%z")
formatted_date = my_date.strftime('%d-%m-%Y')

Sources:

GHJGHJJHG
  • 86
  • 2
  • 3