Python常用模块-时间模块(time & datetime)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.初始time模块
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 7 import time 8 9 """" 10 模块的分类: 11 模块本质就是一个“*.py”文件,大致分为以下三类: 12 1>.内置模块,指的是存在Python解释器内部的模块,如time模块; 13 2>>第三方模块,指的是安装好Python后的lib文件夹中的模块; 14 3>.自定义模块,指的是你自己写的Python程序; 15 """ 16 17 print(time.time()) #返回当前的时间戳,表示从1971年1月1日"00:00:00"到此刻时间节点的秒数。 18 19 s = time.localtime() #创建一个时间对象,也可以说是在结构化时间对象,返回本地时间的struct_time对象格式。 20 print(s) 21 print(s.tm_year) #获取年份。 22 print(s.tm_mon) #获取月份。 23 24 s2 = time.gmtime() #返回utc时间的struc时间对象格式。 25 print(s2) 26 27 28 29 30 #以上代码执行结果如下: 31 1520176127.9244497 32 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=23, tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0) 33 2018 34 3 35 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=15, tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0)
二.时间模块的相互转换
1.转换助记图
2.案例展示
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 7 import time 8 9 s = time.localtime(31245244545) #结构化时间对象,可以将时间戳转换成结构化时间。 10 print(s) 11 12 s2 = time.mktime(time.localtime()) #将结构化时间转换成时间戳。 13 print(s2) 14 15 s3 = time.strftime("%Y-%m-%d",time.localtime()) #将结构化时间转换成字符串时间。 16 print(s3) 17 18 s4 = time.strptime("1993:05:19","%Y:%m:%d") #将字符串时间转换成结构化时间。 19 print(s4) 20 21 22 23 #以上代码执行结果如下: 24 time.struct_time(tm_year=2960, tm_mon=2, tm_mday=15, tm_hour=2, tm_min=35, tm_sec=45, tm_wday=4, tm_yday=46, tm_isdst=0) 25 1520176194.0 26 2018-03-04 27 time.struct_time(tm_year=1993, tm_mon=5, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=139, tm_isdst=-1)
三.time扩充
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 7 8 import time 9 print(time.asctime(time.localtime())) #将结构化时间转换成字符串时间。 10 print(time.ctime(565656446)) #将时间戳转换成字符串时间。 11 12 time.sleep(2) #让程序暂停2秒钟,用于模拟I/O阻塞,并不占用CPU资源。 13 14 15 16 17 #以上代码执行结果如下: 18 Sun Mar 4 23:11:16 2018 19 Sat Dec 5 06:47:26 1987
四.time模块小试牛刀
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 7 import time 8 9 s = "2018-03-04" 10 11 def ChangeTime(string,number,format="%Y-%m-%d"): 12 StructTime = time.strptime(string,format ) 13 print(StructTime) 14 StampTime = time.mktime(StructTime) 15 print(StampTime) 16 NewStamp = StampTime + 3600 * 24 * number 17 NewStringTime = time.strftime(format, time.localtime(NewStamp)) 18 print(NewStringTime) 19 20 ChangeTime(s,3) #推算3日后的时间 21 22 23 24 25 #以上代码执行结果如下: 26 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=63, tm_isdst=-1) 27 1520092800.0 28 2018-03-07
五.datetime介绍
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 import time,datetime 7 8 print(datetime.datetime.now()) #打印当前系统时间 9 10 print(datetime.date.fromtimestamp(time.time())) #时间戳直接转成日期格式如:2018-03-04 11 12 print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 13 14 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 15 16 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 17 18 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 19 20 c_time = datetime.datetime.now() 21 print(c_time.replace(minute=3,hour=2)) ##时间替换 22 23 24 25 26 #以上代码执行结果如下: 27 2018-03-04 23:12:13.477416 28 2018-03-04 29 2018-03-07 23:12:13.477416 30 2018-03-01 23:12:13.477416 31 2018-03-05 02:12:13.477416 32 2018-03-04 23:42:13.477416 33 2018-03-04 02:03:13.477416
六.关于格式化参数的详细说明
Directive | Meaning | Notes |
---|---|---|
%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. | |
%d |
Day of the month as a decimal number [01,31]. | |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (1) |
%S |
Second as a decimal number [00,61]. | (2) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z |
Time zone name (no characters if no time zone exists). | |
%% |
A literal '%' character. |