time模块

time模块

1.时间戳(1970纪元后经过的浮点秒数)
2.时间戳、字符串格式和元组格式时间之间的相互转化
在这里插入图片描述
在这里插入图片描述
参数参照

%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.

代码示例

import time
time.sleep(0.1)             #休眠几秒
print('-----时间戳-----')
print(time.time())          #返回当前时间的时间戳
print('------时区------')
print(time.timezone)        #时区,东八区。28800/3600=8
print('时间戳->元组(UTC)')
print(time.gmtime(92355320))#把时间戳转换成UTC时间的元组格式
print(time.gmtime())        #没有参数默认当前时间。
print('时间戳->元组(本地)')
print(time.localtime())     #把时间戳转换成本地时间的元组格式
x = time.localtime()        #x是元组格式的时间
print(x.tm_year)            #获取时间元组中的tm_year
print('元组->时间戳')
print(time.mktime(x))       #将元组时间转换成时间戳
print('元组->格式化字符串')
print(time.strftime('%Y-%m-%d %H:%M:%S',x)) #将元组格式转换成格式化字符串
y = time.strftime(('%Y-%m-%d %H:%M:%S'),x)
print('格式化字符串->元组')
print(time.strptime(y,'%Y-%m-%d %H:%M:%S')) #将格式化字符串转换成元组
print('元组->特定格式化字符串')
print(time.asctime(x))      #将元组时间转换成字符串格式
print('时间戳->特定格式化字符串')
print(time.ctime(999999999))#将时间戳转换成字符串格式

运行结果

-----时间戳-----
1579251490.504984
------时区------
-28800
时间戳->元组(UTC)
time.struct_time(tm_year=1972, tm_mon=12, tm_mday=4, tm_hour=22, tm_min=15, tm_sec=20, tm_wday=0, tm_yday=339, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=8, tm_min=58, tm_sec=10, tm_wday=4, tm_yday=17, tm_isdst=0)
时间戳->元组(本地)
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=16, tm_min=58, tm_sec=10, tm_wday=4, tm_yday=17, tm_isdst=0)
2020
元组->时间戳
1579251490.0
元组->格式化字符串
2020-01-17 16:58:10
格式化字符串->元组
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=17, tm_hour=16, tm_min=58, tm_sec=10, tm_wday=4, tm_yday=17, tm_isdst=-1)
元组->特定格式化字符串
Fri Jan 17 16:58:10 2020
时间戳->特定格式化字符串
Sun Sep  9 09:46:39 2001
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值