python培训之如何格式化日期格式

发布时间:2022-11-05 09:35:51 人气:144 作者:多测师

  Python中日期格式化是非常常见的操作,Python中能用很多方式处理日期和时间,转换日期格式是一个常见的功能。Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。

  时间间隔是以秒为单位的浮点小数。每个时间戳都以自从格林威治时间1970年01月01日00时00分00秒起经过了多长时间来表示。

  注: 以下代码在Python3下运行通过, Python2下未经测试

  将字符串的时间转换为时间戳

  import time

  t = "2017-11-24 17:30:00"

  #将其转换为时间数组

  timeStruct = time.strptime(t, "%Y-%m-%d %H:%M:%S")

python培训之如何格式化日期格式

  #转换为时间戳:

  timeStamp = int(time.mktime(timeStruct))

  print(timeStamp)

  结果为:

  1511515800

  时间戳转换为指定格式日期

  timeStamp = 1511515800

  localTime = time.localtime(timeStamp)

  strTime = time.strftime("%Y-%m-%d %H:%M:%S", localTime)

  print(strTime)

  结果为:

  2017-11-24 17:30:00

  以上内容为大家介绍了python培训之如何格式化日期格式,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线