python的time模块实战总结

版本:

  • Python2
  • Python3

Python的time模块中时间表现的形式有三种

  • struct_time时间元组
  • timestamp时间戳
  • 格式化的时间字符串

时间(struct_time)

类似于new一个时间实例
适合获取年月日时分秒,以及其他操作等等,
通过time.localtime()可以生成一个struct_time

时间戳(timestamp)

顾名思义,就是一个时间戳,单位是秒
适合进行时间的加减,time模块不能直接对时间进行加减,比如说加1个小时,1天,只能通过时间戳进行加减

字符串

适合打印输出

相互转化

转化关系

python的time模块的时间实战总结_01

字符串与时间之间的转换

字符串转时间
print(
        (
            "字符串转时间: time.strptime(\"2019-10-08 20:30:00\", \"%Y-%m-%d %H:%M:%S\")"
            " --> {}"
        ).format(
            time.strptime("2019-10-08 20:30:00", "%Y-%m-%d %H:%M:%S")
        )
    )

返回结果:

字符串转时间: 
time.strptime("2019-10-08 20:30:00", "%Y-%m-%d %H:%M:%S") --> 
time.struct_time(
	tm_year=2019, 
	tm_mon=10, 
	tm_mday=8, 
	tm_hour=20, 
	tm_min=30, 
	tm_sec=0, 
	tm_wday=1, 
	tm_yday=281, 
	tm_isdst=-1
)
时间转字符串
t = time.localtime()
    print("时间转字符串: time.strftime(\"%Y-%m-%d %H:%M:%S\", t) --> {}".format(
        time.strftime("%Y-%m-%d %H:%M:%S", t)
    ))

返回结果:

时间转字符串: time.strftime("%Y-%m-%d %H:%M:%S", t) --> 2019-10-08 14:22:31

python的time模块的时间实战总结_02

时间与时间戳之间的转换

时间转时间戳
t = time.localtime()
print("时间转时间戳: time.mktime(t) --> {}".format(time.mktime(t)))

返回结果:

时间转时间戳: time.mktime(t) --> 1570515751.0
时间戳转时间
sec = time.time()
print("时间戳转时间: time.localtime(sec) --> {}".format(time.localtime(sec)))

返回结果:

时间戳转时间: time.localtime(sec) --> time.struct_time(
	tm_year=2019,
	tm_mon=10, 
	tm_mday=8, 
	tm_hour=14, 
	tm_min=22, 
	tm_sec=31, 
	tm_wday=1, 
	tm_yday=281, 
	tm_isdst=0
)

python的time模块的时间实战总结_03

字符串与时间戳之间的转换

字符串与时间戳之间的转换需要先转成时间类再向目标类型转换

字符串转时间戳

转换过程: 字符串 --> 时间 --> 时间戳

f = "2019-10-03 10:45:00"
df = "%Y-%m-%d %H:%M:%S"
t = time.strptime(f, df)
sec = time.mktime(t)
print("字符串转时间戳: sec = {}".format(sec))

返回结果:

字符串转时间戳: sec = 1570070700.0
时间戳转字符串

时间戳 --> 时间 --> 字符串

sec = 1570070700
t = time.localtime(sec)
time_str = time.strftime("%Y-%m-%d %H:%M:%S", t)
print("时间戳转字符串: time_str= {}".format(time_str))

返回结果:

时间戳转字符串: time_str= 2019-10-03 10:45:00

python的time模块的时间实战总结_04

获取当时时间

获取当时间

time.localtime()

获取当时时间的时间戳

time.time()

获取当时时间特定格式的字符串

这种格式的时间字符串我们中国地区用得比较少,可以忽略不计

time.asctime()
time.ctime()

返回结果:

time.asctime() --> Tue Oct  8 14:30:49 2019
time.ctime() --> Tue Oct  8 14:30:49 2019

python的time模块的时间实战总结_05

时间的加减

time模块时间的加减只能通过timestamp加减对应的秒数,然后再转回去
如果给定的是时间字符串的话
只能先把字符串先转为时间, 再转为时间戳,
然后对时间戳进行加减, 加减过后再转回时间,再转回字符串
这样挺麻烦的,推荐使用datetime模块

下面给出一个时间字符串加3个小时,并打印结果:

t = time.strptime("2019-10-03 10:45:00", "%Y-%m-%d %H:%M:%S")
sec = time.mktime(t)
sec += 3 * 60 * 60
t1 = time.localtime(sec)
s1 = time.strftime("%Y-%m-%d %H:%M:%S", t1)
print("加3小时后的时间为: {}".format(s1))

返回结果:

原时间为: 2019-10-03 10:45:003小时后的时间为: 2019-10-03 13:45:00

python的time模块的时间实战总结_06

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值