anon anon - 4 months ago 16x
MySQL Question

Convert Mysql timestamp to Python's datetime

I have a table in MySQL that has a column with

This is what the value on that column looks like on MySQL.

enter image description here

When I try to fetch that
column using Python, this is the result I get.

Tue Apr 26 03:10:02 SGT 2016

This is the code I have for converting it to the same format as the one from MySQL
print mysql_time

mysql_time = 'Tue Apr 26 03:10:02 SGT 2016'
print datetime.datetime.strptime(str(mysql_time), '%a %b %m %H:%M:%S %Z %Y').strftime("%Y-%m-%d %H:%M:%S")

This is the result I get:

2018-02-06 03:10:02

Can anyone shed some light as to why it is different from the one I fetch from MySQL?


You're using %m as the third argument which is month. There is no 26th month, so it is advancing the time forward to compensate.

This should most likely be %d.