• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
菩提叶子
博客园    首页    新随笔    联系   管理    订阅  订阅
python time模块

time时间模块

一、time()获取本地时间戳

res = time.time()

二、localtime()获取本地时间元组

res = time.localtime()

三、mktime通过时间元祖获取时间戳

ttp = (2020,7,28,10,48,30,0,0,0)
res = time.mktime(ttp)

四、ctime获取本地时间字符串

res = time.ctime()#默认获取当前时间戳时间字符串

五、asctime通过时间元组获取时间字符串

ttp = (2020,7,28,10,54,30,6,0,0)
res = time.asctime(ttp)

六、sleep 休眠时间,单位是秒,可以使浮点数

print('开始')
time.sleep(3.3)
print('结束')

七、strftime将时间格式输出为字符串

time.strftime("%Y-%m-%d %H:%M:%S")

八、strptime将时间字符串输出位struct_time

time.strptime("2018-1-26 12:55:20",'%Y-%m-%d %H:%M:%S')

九、时间格式控制符

时间格式控制符 说明
%Y 四位数的年份,取值范围为0001~9999,如1900
%m 月份(01~12),例如10
%d 月中的一天(01~31)例如:25
%B 本地完整的月份名称,比如January
%b 本地简化的月份名称,比如Jan
%a 本地简化的周日期,Mon~Sun,例如Wed
%A 本地完整周日期,”Monday~Sunday,例如Wednesday
%H 24小时制小时数(00~23),例如:12
%l 12小时制小时数(01~12),例如:7
%p 上下午,取值为AM或PM
%M 分钟数(00~59),例如26
%S 秒(00~59),例如26

posted on 2022-10-25 16:12  菩提叶子  阅读(60)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3