Python datetime字符串/时间戳相互转换


复制代码
#datetime转字符串
ex:
  now = datetime.datetime.now()
  time_string = now.strftime('%Y-%m-%d %H:%M:%S')

#字符串转datetime
ex:
  time_string = '2020-05-26 11:42:54'
  datetime.datetime.strptime(time_string, "%Y-%m-%d %H:%M:%S")

#字符串转时间戳
ex: 
  time_string = '2020-05-26 11:42:54'
  time.mktime(time.strptime(time_string, "%Y-%m-%d %H:%M:%S"))
#时间戳转字符串 ex:
  stamp = 1590464574.0
  time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp))
#datetime转时间戳 ex:
  now = datetime.datetime.now()
  time.mktime(now.timetuple())
复制代码

 

posted @ 2020-10-16 16:31  BobPong  阅读(591)  评论(0编辑  收藏  举报