将时间戳转换成时间的一个方法

朱雀 2022-01-09 07:45 576阅读 0赞
  1. 1 import time
  2. #将时间戳转换为标准时间格式
  3. 2 def timestamp_to_fomat(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
  4. 3 #默认返回当前格式化好的时间
  5. 4 #传入时间戳的话,把时间戳转换成格式化好的时间,返回
  6. 5 if timestamp:
  7. 6 time_tuple = time.localtime(timestamp)
  8. 7 res = time.strftime(format,time_tuple)
  9. 8 else:
  10. 9 res = time.strftime(format)#默认读取当前时间
  11. 10 return res
  12. #将标准时间转换为时间戳
  13. 1 def strToTimestamp(str=None,format='%Y-%m-%d %H:%M:%S'):
  14. 2 if str:
  15. 3 tp = time.strptime(str,format)#转成时间元组
  16. 4 res = time.mktime(tp)#再转成时间戳
  17. 5 else:
  18. 6 res = time.time()#默认获取当前时间戳
  19. 7 return int(res)

转载于:https://www.cnblogs.com/jial-test/p/8944477.html

发表评论

表情:
评论列表 (有 0 条评论,576人围观)

还没有评论,来说两句吧...

相关阅读