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())

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzyjzs.html