Zahidul Hossein Ripon
Zahidul Hossein Ripon

Reputation: 672

Convert timestamp to mysql friendly format in python

I got "timestamp": "2018-10-28T12:49:00.816Z" from a API response. How can I convert 2018-10-28T12:49:00.816Z to %Y-%m-%d %H:%M:%S format.I tried datetime.strptime

Upvotes: 0

Views: 154

Answers (1)

Christian Sloper
Christian Sloper

Reputation: 7510

This is suprisingly easy using dateutil

import dateutil

dateutil.parser.parse("2018-10-28T12:49:00.816Z")

output:

datetime.datetime(2018, 10, 28, 12, 49, 0, 816000, tzinfo=tzlocal())

or directly to your format:

dateutil.parser.parse("2018-10-28T12:49:00.816Z").strftime('%Y-%m-%d %H:%M:%S')

output:

 '2018-10-28 12:49:00'

Upvotes: 4

Related Questions