Python时间与时间戳转换

@lianst  April 23, 2017

python的datetime转换为UNIX时间戳

#导入time模块(用于转换时间戳)
In [1]: import time

# 导入datetime模块(用于获取当前标准时间)
In [2]: import datetime

#获取当前时间并赋值给变量what_Time
In [3]: what_Time = datetime.datetime.now()

#打印what_Time
In [4]: print what_Time
2017-04-24 08:56:31.096301  

#转换UNIX时间戳,并把结果赋值给unix_Timestamp变量
In [5]: unix_Timestamp = time.mktime(what_Time.timetuple())

#打印unix_Timestamp变量的结果,得到10位的unix时间戳。
In [6]: print unix_Timestamp
1493038591.0

把unix时间戳转换为python的datetime时间


In [7]: Now_Time = datetime.datetime.fromtimestamp(unix_Timestamp)        

In [8]: print Now_Time
2017-04-24 08:56:31




添加新评论