time模块常用方法

time.time()

生成一个float类型的时间戳
在这里插入图片描述

time.localtime()
  • 返回当前的时间类型为struct_time类型的时间
import time
t1 = time.localtime()

print(t1, type(t1))

#time.struct_time(tm_year=2020, tm_mon=1, tm_mday=23, tm_hour=16, tm_min=51, tm_sec=15, tm_wday=3, tm_yday=23, tm_isdst=0) <class 'time.struct_time'>
  • 可以接受一个为整数的时间戳类型的时间
import time
t1 = time.localtime(int(time.time()))
print(t1, type(t1))
#time.struct_time(tm_year=2020, tm_mon=1, tm_mday=23, tm_hour=16, tm_min=54, tm_sec=39, tm_wday=3, tm_yday=23, tm_isdst=0) <class 'time.struct_time'>

time.asctime()
  • 返回当前时间的这种格式的字符串
    在这里插入图片描述
time.strftime()
  • 传入格式struct_time,返回按照format格式格式化后的时间字符串
t5 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(t5, type(t5))

在这里插入图片描述
格式占位符参考

    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
time.strptime()
  • 将字符串时间转化为struct_time,time.strftime()的一个逆向操作
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值