import time
from datetime import datetime
import pytz
In [14]: t = datetime.fromtimestamp(1546751128, pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
In [15]: t
Out[15]: '2019-01-06 13:05:28'
Last active
January 6, 2019 06:34
-
-
Save g10guang/6be88f1eea91aadd5ddd47921d90ca80 to your computer and use it in GitHub Desktop.
Python unix time and strftime convert
import time
from datetime import datetime
format = '%Y-%m-%d %H:%M:%S'
In [46]: time.mktime(datetime.strptime('2018-11-23 12:00:00', format).timetuple())
Out[46]: 1542945600.0
In [53]: datetime.utcfromtimestamp(time.time())
Out[53]: datetime.datetime(2018, 11, 25, 11, 52, 9, 527595)
In [57]: int(d.now().strftime('%s'))
Out[57]: 1543146992
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I google it every time I convert a timestamp to string format. So I mark it down.