1.time模块
操作如下:
import time
#。time()获取时间戳
print(time.time())#当前时间戳
#.localtime(timestamp)获取结构形式时间(本地时区)
print(time.localtime())#当前时间的struct_ time形式
print(time.localtime(0))#时间戳为0的struct_time形式
print(time.localtime().tm_year)#获取当前年度
#.gmtime(timestamp)获取0时区结构时间 ,传入参数为时间戳,默认为当前时间戳
print(time.gmtime())
print(time.gmtime(0))
#########https://www.cnblogs.com/tianyiliang/p/7789185.html参考
#通过strptime(string,format)确认本地格式某一天对应的struct_time,从而可以简单获取星期和天数
struct_time=time.strptime("2020年6月11日22点32分42秒","%Y年%m月%d日%H点%M分%S秒")
print(struct_time)
#通过strftime(format,struct_time)将已知的struct_time转化为格式化形式
# print(time.strftime("%Y年%m月%d日%H点%M分%S秒", time.localtime()))
#上一句会报错,UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error
#无法解析汉字的编码,可以通过如下方式解决
print(time.strftime("%Y{y}%m{m}%d{d}%H{h}%M{M}%S{s}", time.localtime()).format(y="年", m="月", d="日", h="点", M="分", s="秒"))
#asctime(struct_time)转换为标准格式时间
print(time.asctime(time.localtime()))
#ctime(timeStamp)将时间戳转化为标准格式时间
print(time.ctime(time.time()))
#sleep(int)进行休眠操作
#clock()返回两次clock之间的时间,第一次调用时计时程序头到调用用时的时间
#clock()在3.3中已被取代,time.perf_counter() time.process_time()
#time.perf_counter()从程序开始一直计时,不会重置
#time.process_time()同样会不会重置,但是此方法不会计入休眠sleep()时间,只计算
time.sleep(5)
print(time.perf_counter())
print(time.process_time())
time.sleep(5)
print(time.perf_counter())
print(time.process_time())
结果如下:
D:\python\python.exe D:/python/Pylearning/day04/Test01.py
1591965523.155573
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=12, tm_hour=20, tm_min=38, tm_sec=43, tm_wday=4, tm_yday=164, tm_isdst=0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
2020
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=12, tm_hour=12, tm_min=38, tm_sec=43, tm_wday=4, tm_yday=164, tm_isdst=0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=11, tm_hour=22, tm_min=32, tm_sec=42, tm_wday=3, tm_yday=163, tm_isdst=-1)
2020年06月12日20点38分43秒
Fri Jun 12 20:38:43 2020
Fri Jun 12 20:38:43 2020
5.0584455
0.078125
10.0585813
0.078125
注意事项:
1.两条操作流程
timestamp→struct_time→日常使用形式日期:time()→localtime()→striftime()
日常使用形式日期→struct_time→timestamp:
striptime()→mktime()
2.striftime()自定义格式化时不支持汉字,可以用如下方法解决:
time.strftime("%Y{y}%m{m}%d{d}%H{h}%M{M}%S{s}", time.localtime()).format(y="年", m="月", d="日", h="点", M="分", s="秒")
3.clock()方法在3.3之后被取代,
#time.perf_counter()从程序开始一直计时,不会重置
#time.process_time()同样会不会重置,但是此方法不会计入休眠sleep()时间,只计算CPU时间
4.参考链接:https://blog.csdn.net/mingming20547/article/details/93471464
https://www.cnblogs.com/tianyiliang/p/7789185.html
2.datetime模块
操作演示:
import datetime
#############date对象
#1.date()的构造函数
# 传入三个int数,分别为年份,月份,日。
import time
print(datetime.date(2018, 1, 1))
#2.today()方法,返回当天日期
print(datetime.date.today())
#3.weekday()方法,返回日期对应星期(0,1,2,3,4,5,6对应星期一、2、3、4、5、6、7)
print(datetime.date(2018,1,2).weekday())
#4.isoformat()方法,返回ISO格式日期,直接打印就是调用此方法
print(datetime.date(2018, 1, 1).isoformat())
#5.striftime(formatString)方法,可以将返回目的格式的日期,同样用format语句处理汉字问题
print(datetime.date(2018, 1, 1).strftime("%Y{y}%m{m}%d{d}").format(y="年", m="月", d="日"))
##########time对象
#1.time()构造函数可以传入时、分、秒、微妙等,都是可选参数
print(datetime.time(hour=8, minute=20, second=20))
#2.isoformat()方法返回标准格式,直接打印就调用这个方法
print(datetime.time(8, 20, 20).isoformat())
#3.striftime(formatString)返回自定义格式时间
print(datetime.time(8, 20, 20).strftime("%H{H}%M{M}%S{S}").format(H="点", M="分", S="秒"))
#########datetime对象(date和time对象的结合体)
#1.构造函数datetime(),必参数:年、月、日 其它为可选参数
print(datetime.datetime(2018, 1, 1, 8, 20, 20))
#2.today()返回一个本地时间的datetime对象
#.now()也会返回
print(datetime.datetime.now())
print(datetime.datetime.today())
#3.fromtimestamp(timestamp)根据时间戳创建一个datetime对象
print(datetime.datetime.fromtimestamp(time.time()))
#4.date()方法获取date对象,time()方法获取time对象
print(datetime.datetime.today().date())
print(datetime.datetime.today().time())
#5.strftime(formatString)自定义格式化
print(datetime.datetime.today().strftime("%Y{Y}%m{m}%d{d}%H{H}%M{M}%S{S}").format(Y="年", m="月", d="日", H="点", M="分",
S="秒"))
运行结果:
2018-01-01
2020-06-12
1
2018-01-01
2018年01月01日
08:20:20
08:20:20
08点20分20秒
2018-01-01 08:20:20
2020-06-12 20:57:43.843346
2020-06-12 20:57:43.843347
2020-06-12 20:57:43.843347
2020-06-12
20:57:43.843347
2020年06月12日20点57分43秒
Process finished with exit code 0