冯纪忠雄,Less Is More
Python中根据time对象的时间戳生成datetime对象,
使用“datetime.datetime.fromtimestamp(timestamp)”即可:
>>> import datetime >>> import time >>> t = time.time() >>> t 1590997335.330784 >>> datetime.datetime.fromtimestamp( t ) datetime.datetime(2020, 6, 1, 15, 42, 15, 330784)
运行一下
Python中根据datetime对象生成time时间戳对象,
则要区分python2和python3,方法并不一样。
Python3中使用
使用“datetime.datetime.timestamp( datetime obj )”即可:
>>> import datetime >>> d = datetime.datetime.now() >>> datetime.datetime.timestamp( d ) 1590998277.916694
Python2的datetime.datetime对象并没有timestamp方法,需要手动计算出来,
“time.mktime( d.timetuple() ) + d.microsecond / 1E6”即可:
>>> import datetime, time >>> d = datetime.datetime.now() >>> time.mktime( d.timetuple() ) + d.microsecond / 1E6 1590998339.12304