anon anon - 5 months ago 22
MySQL Question

Convert Mysql timestamp to Python's datetime

I have a table in MySQL that has a column with

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

enter image description here

When I try to fetch that
InstallTs
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?

Answer

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.

Comments