python中时间与时间戳转换

import time
import datetime

#时间转换为时间戳
def date_to_timestamp(date_string): return time.mktime(datetime.datetime.strptime(date_string, '%Y-%m-%d').timetuple())
#时间戳转换为时间
def timestamp_to_date(timestamp): 

  if timestamp is None:
   
return None

return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')


if __name__ == '__main__':

  a
=date_to_timestamp('2018-06-22')
  print(a)
  b
=timestamp_to_date(1529596800.0)
  print(b)

打印日志:

C:\Python34\python.exe "E:/consult project/automation_seat/automation/test/count.py"
1529596800.0
2018-06-22 00:00:00

Process finished with exit code 0

 



posted @ 2019-07-15 22:43  做一只热爱生活的小透明  阅读(218)  评论(0)    收藏  举报