time模块
import time
print(time.time())#获取时间戳 浮点数
print(time.sleep(2))#睡眠,阻塞
print(time.strftime('%Y-%m-%d %H:%M:%S'))#时间显示格式,页面显示的格式 结构化转字符串
print(time.gmtime())#结构化时间,元祖的形式(命名元祖),可以根据索引和元素进行查找 UTC时间,与英国伦敦当地时间一致
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=25, tm_hour=8, tm_min=10, tm_sec=50, tm_wday=3, tm_yday=206, tm_isdst=0)
#time.localtime获取的是当地时间
# print(time.gmtime()[0])
# print(time.gmtime().tm_year)
#将时间戳转换成字符串时间
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(1564028611.631374)))
#将字符串时间转换成时间戳
print(time.mktime(time.strptime("2024-3-16 12:30:30","%Y-%m-%d %H:%M:%S")))
---------------------------------------
重点:
# time重点:
# time.time()
# time.sleep()
# time.gmtime() / time.localtime() #
# time.strftime("格式化","结构化时间") #
# time.strptime("字符串","格式化")
# time.mktime()
5.3 datetime模块
print(type(datetime.now()))获取当前时间,获取的是一个对象
print(datetime(2019,5,21,15,14,00) - datetime(2019,5,20,14,20,00))计算时间差
# 将当前时间转化成时间戳
# t = datetime.now()
# print(t.timestamp())
-----------------------------------------
# 将时间戳转化成当前时间
# import time
# print(datetime.fromtimestamp(15000000000))
-----------------------------------------------------
# 将字符串转成对象
# print(type(datetime.strptime("2019-10-10 22:23:24","%Y-%m-%d %H:%M:%S")))
-----------------------------------------------------
# 将对象转成字符串
# print(str(datetime.now()))
# print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
------------------------------------------------------
# datetime加减
# print(datetime.now() + timedelta(hours=30 * 24 * 12))
# print(datetime.now() - timedelta(hours=30 * 24 * 12))
***********************************************************
# datatime模块
import datetime
now_time = datetime.datetime.now() # 现在的时间
# 只能调整的字段:weeks days hours minutes seconds
print(datetime.datetime.now() + datetime.timedelta(weeks=3)) # 三周后
print(datetime.datetime.now() + datetime.timedelta(weeks=-3)) # 三周前
print(datetime.datetime.now() + datetime.timedelta(days=-3)) # 三天前
print(datetime.datetime.now() + datetime.timedelta(days=3)) # 三天后
print(datetime.datetime.now() + datetime.timedelta(hours=5)) # 5小时后
print(datetime.datetime.now() + datetime.timedelta(hours=-5)) # 5小时前
print(datetime.datetime.now() + datetime.timedelta(minutes=-15)) # 15分钟前
print(datetime.datetime.now() + datetime.timedelta(minutes=15)) # 15分钟后
print(datetime.datetime.now() + datetime.timedelta(seconds=-70)) # 70秒前
print(datetime.datetime.now() + datetime.timedelta(seconds=70)) # 70秒后
current_time = datetime.datetime.now()
# 可直接调整到指定的 年 月 日 时 分 秒 等
print(current_time.replace(year=1977)) # 直接调整到1977年
print(current_time.replace(month=1)) # 直接调整到1月份
print(current_time.replace(year=1989,month=4,day=25)) # 1989-04-25 18:49:05.898601
# 将时间戳转化成时间
print(datetime.fromtimestamp(1232132131)) # 2009-01-17