python时间处理

390阅读 0评论2023-04-26 zenith518
分类:LINUX

转换格式
两个重要方法
formatted_timestring = strftime( timeformattedstring )
datetimeobj = strptime( formatted_timestring )

点击(此处)折叠或打开

  1. from datetime import datetime
  2. # 获取当前时间
  3. now = datetime.now()
  4. # 将当前时间转换为所需格式
  5. formatted_time = now.strftime("%Y%m%d%H%M%S")
  6. # 打印结果 print(formatted_time)
转换成datetimeobj的好处是可以实现各种日期时间的计算功能。
{BANNED}最佳后用strftime 按指定格式输出。

上一篇:python老版本操作经验贴
下一篇:技术界也开始玩猫捉老鼠的游戏了