如何快速用手机做网站,提出网站推广途径和推广要点,网站怎么做镜像,紫光华宇拼音输入法官方下载文章目录 struct_time元组python中时间日期格式化符号#xff1a; 时间#xff08;Time#xff09;模块日期#xff08;Calendar#xff09;模块其他相关模块和函数 Python 提供了一个 time 和
calendar 模块可以用于格式化日期和时间。 时间间隔是以秒为单位的浮点小数。… 文章目录 struct_time元组python中时间日期格式化符号 时间Time模块日期Calendar模块其他相关模块和函数 Python 提供了一个 time 和
calendar 模块可以用于格式化日期和时间。 时间间隔是以秒为单位的浮点小数。 每个时间戳secs)都以自从1970年1月1日午夜历元经过了多长时间来表示。 struct_time元组
很多Python函数用一个元组装起来的9组数字处理时间也就是struct_time元组。这种结构具有如下属性 python中时间日期格式化符号
%y 两位数的年份表示00-99%Y 四位数的年份表示000-9999%m月份01-12%d 月内中的一天0-31%H24小时制小时数0-23%I 12小时制小时数01-12%M 分钟数0059%S 秒00-59%a 本地简化星期名称%A 本地完整星期名称%b 本地简化的月份名称%B 本地完整的月份名称%c 本地相应的日期表示和时间表示%j 年内的一天001-366%p 本地A.M.或P.M.的等价符%U 一年中的星期数00-53星期天为星期的开始%w 星期0-6星期天为星期的开始%W 一年中的星期数00-53星期一为星期的开始%x 本地相应的日期表示%X 本地相应的时间表示%Z 当前时区的名称%% %号本身 时间Time模块 1、time.time() 返回当前时间的时间戳secs1970纪元后经过的浮点秒数。
import time
print(time.time()) #输出1580462925.8417437时间戳可用于time()模板的其他函数localtimegmtimesleep等等 2、time.localtime() 接收时间戳并返回时间戳的当地时间下的时间元组。若参数为空返回当地时间下的时间元组
import time
secstime.time() #时间戳
print(time.localtime()) #参数为空返回当地时间的当前时间元组
print(time.localtime(secs)) #返回指定时间戳的当地时间的时间元组3、time.gmtime() 接收时间戳返回格林威治天文时间下的时间元组。
import time
secstime.time() #时间戳
print(time.localtime(secs))
print(time.gmtime(secs))locatime()和gmtime()接受的同一个时间戳secs)返回的时间元组却不一样这就是时差导致的 4、time.asctime([tupletime])与 time.strftime(fmt[,tupletime]) time.asctime([tupletime])接受时间元组并返回一个可读的形式为”Tue Dec 11 18:07:14 2008”2008年12月11日 周二18时07分14秒的24个字符的字符串。 time.strftime(fmt[,tupletime])接收以时间元组并返回以可读字符串表示的当地时间格式由fmt决定。 两者的区别就是格式不一样第二个函数格式由fmt决定
import time
secstime.time() #时间戳
atime.localtime(secs) #当地时间的时间元组
btime.gmtime(secs) #格林威治时间的时间元组print(time.asctime(a)) #获得时间的字符串
print(time.asctime(b)) #获得时间的字符串
print(time.strftime(%Y-%m-%d %H:%M:%S,a)) #获得格式化(指定格式)的时间字符串运行结果
Fri Jan 31 17:45:24 2020
Fri Jan 31 09:45:24 2020
2020-01-31 17:45:245、time.sleep() 推迟调用线程的运行
import time
print(start:,time.ctime())
time.sleep(5) #从这一行推迟5秒进程
print(end:,time.ctime())日期Calendar模块 1、calendar.calendar(year,w2,l1,c6) 返回一个多行字符串格式的year年年历3个月一行间隔距离为c。 每日宽度间隔为w字符。每行长度为21* W182* C。l是每星期行数。
import calendar
print(calendar.calendar(2020,w2,l1,c6))运行结果 2020January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su1 2 3 4 5 1 2 16 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 2930 31April May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su1 2 3 4 5 1 2 3 1 2 3 4 5 6 76 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30July August September
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su1 2 3 4 5 1 2 1 2 3 4 5 66 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 3031October November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su1 2 3 4 1 1 2 3 4 5 65 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 3130进程已结束,退出代码0 2、calendar.firstweekday( ) 返回当前每周起始日期的设置。默认情况下首次载入 calendar 模块时返回 0即星期一。
import calendar
print(calendar.firstweekday()) #输出03、calendar.isleap(year) 是闰年返回 True否则为 False。
import calendar
print(calendar.isleap(2020)) #输出True4、calendar.leapdays(y1,y2) 返回在Y1Y2两年之间的闰年总数
import calendar
print(calendar.leapdays(2020,2100)) #输出205、calendar.month(year,month,w2,l1) 返回一个多行字符串格式的year年month月日历两行标题一周一行。每日宽度间隔为w字符。每行的长度为7* w6。l是每星期的行数。
import calendar
print(calendar.month(2020,2,w1,l1)) 运行结果
February 2020
Mo Tu We Th Fr Sa Su1 23 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 296、calendar.monthcalendar(year,month) 返回一个整数的单层嵌套列表。每个子列表装载代表一个星期的整数。Year年month月外的日期都设为0;范围内的日子都由该月第几日表示从1开始。
import calendar
print(calendar.monthcalendar(2020,2))运行结果 7、calendar.monthrange(year,month) 返回两个整数。第一个是该月的星期几的日期码第二个是该月的日期码。日从0星期一到6星期日;月从1到12。
import calendar
print(calendar.monthrange(2020,5)) #输出(4, 31)8、calendar.prcal(year,w2,l1,c6) 相当于 print calendar.calendar(2020,w2,l1,c6)。
import calendar
calendar.prcal(2020,w2,l1,c6)9、calendar.prmonth(year,month,w2,l1) 相当于 print calendar.month(2020,2,w2,l1) 。
import calendar
calendar.prmonth(2020,2,w3,l1)10、calendar.setfirstweekday(weekday) 设置每周的起始日期码。0星期一到6星期日
import calendar
calendar.setfirstweekday(3)
print(calendar.firstweekday()) #输出3若没有第二条语句则输出011、calendar.timegm(tupletime) 和time.gmtime相反接受一个时间元组形式返回该时刻的时间戳1970纪元后经过的浮点秒数。
import calendar
import time
atime.time()
print(gmtime:,time.gmtime(a))#gmtime用法
tupletime (2020, 1, 31, 16, 34, 30)
print(calendar.timegm(tupletime))#timegm用法12、calendar.weekday(year,month,day) import calendar
print(calendar.weekday(2020,1,31)) #输出42020年1月31日是周五其他相关模块和函数
在Python中其他处理日期和时间的模块还有
datetime模块pytz模块dateutil模块