python 之时间模块
1. python的垃圾回收(gc)2.python3 的八大数据类型3.Python 七类常见运算符4.python流程控制5.字符编码与字节流二进制补码形式6.python 之with open7.python 函数之传参8.python 函数之作用域、闭包9. python 装饰器10.递归、二分查找、冒泡排序11.三元表达式、列表、字典生成式、匿名函数12.迭代器和生成器、异常捕获13.python 常用的内置函数14.python模块的导入15.python 之 re (regexp expression)
16.python 之时间模块
17.python 随机模块random18.python json序列化模块19.hashlib 模块20.python 之logging 模块21.Python 深拷贝和浅拷贝详解22.面向对象编程之类和对象的定义23.面向对象编程之绑定方法、掩藏属性、装饰器24.面向对象之对象的三大特性25.python 魔术方法26.python 之反射27.Mixins机制和元类28.python 之http、tcp/ip29.基于tcp协议的socket编程30.python 并发编程之进程31.python 并发编程之线程32.GIL锁,互斥锁33.python并发编程之协程34.进程池和线程池35.MacOS13 m1 安装 mysql8.0.3236.mysql8 增删改查、约束条件37.mysql8 查询关键字、多表关系38.多表联合查询、脚本使用pymysql39.mysql8 索引、视图、事务、存储过程、触发器40.前端三剑客之HTML41.前端三剑客之CSS( position位置固定)42.前端三剑客之JavaScript 43.html 之 jQuery库、阻止二次提交44.html 之 Bootstrap45.python 安装pip3、虚拟环境、pip3下载离线包46.开发设计模式之单例模式47.celery 任务队列、双写一致性、异步秒杀48.py操作七牛云存储、minio、fastdfs49.支付宝支付50.路飞学城上线51.python之任务调度(APScheduler和schedule)52.命令行帮助信息、手动和交互执行53.python之csv、openpyxl/pandas54.python 并发整理一、time 模块
1、三个时间显示形式
- timestamp:时间戳
- 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
- struct_time:结构化时间
- struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
- format_string:格式化时间
- 格式化的时间字符串(Format String): ‘1999-12-06’
2、时间戳
1 2 3 | print (time.time()) 1686297616.068288 |
3、结构化时间
time.struct_time
是 Python 中的一个元组,用于表示时间的各个组成部分,例如年、月、日、时、分、秒等。它常用于与时间相关的操作和函数,如时间的格式化、解析和计算等。
1 2 3 4 5 6 7 8 9 10 | res = time.localtime() print (res) # 输出 time.struct_time(tm_year = 2023 , tm_mon = 6 , tm_mday = 9 , tm_hour = 16 , tm_min = 8 , tm_sec = 20 , tm_wday = 4 , tm_yday = 160 , tm_isdst = 0 ) print (res.tm_year, res[ 0 ]) # 调用年月日、小时 print (res.tm_mon, res[ 1 ]) print (res.tm_mday, res[ 2 ]) print (res.tm_hour, res[ 3 ]) |
4、格式化时间
1 2 3 4 5 6 7 | res = time.strftime( '%Y-%m-%d %H:%M:%S' ) # res = time.strftime('%Y-%m-%d %X') # 与上面的等价 print (res) # 输出 2023 - 06 - 09 16 : 12 : 19 |
格式化字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | % y 两位数的年份表示( 00 - 99 ) % Y 四位数的年份表示( 000 - 9999 ) % m 月份( 01 - 12 ) % d 月内中的一天( 0 - 31 ) % H 24 小时制小时数( 0 - 23 ) % I 12 小时制小时数( 01 - 12 ) % M 分钟数( 00 = 59 ) % 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 当前时区的名称 % % % 号本身 |
小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的
5、几种格式之间的转换
时间戳-->结构化时间
1 2 3 4 5 6 7 8 9 | #1、time.gmtime(时间戳) #UTC时间,与英国伦敦当地时间一致 print (time.gmtime(time.time())) # 输出 time.struct_time(tm_year = 2023 , tm_mon = 6 , tm_mday = 9 , tm_hour = 8 , tm_min = 18 , tm_sec = 39 , tm_wday = 4 , tm_yday = 160 , tm_isdst = 0 ) # 2、time.localtime(时间戳) print (time.localtime(time.time())) |
结构化时间-->时间戳
1 2 3 4 5 6 7 | # time.mktime(结构化时间) # print(time.time()) time_tuple = time.localtime( 1686298960.813175 ) print (time.mktime(time_tuple)) # 输出 1686298960.0 |
结构化时间-->字符串时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # time.strftime("格式定义","结构化时间") 结构化时间参数若不传,则显示当前时间 res = time.strftime( "%Y-%m-%d %X" ) print (res) # 输出 2023 - 06 - 09 16 : 25 : 37 # time.strftime 接受参数时,输出转换成第二个参数的时间 res = time.strftime( "%Y-%m-%d" , time.localtime( 1500000000 )) print (res) #输出 2017 - 07 - 14 #字符串时间-->结构化时间 #time.strptime(时间字符串,字符串对应格式) >>>time.strptime( "2017-03-16" , "%Y-%m-%d" ) time.struct_time(tm_year = 2017 , tm_mon = 3 , tm_mday = 16 , tm_hour = 0 , tm_min = 0 , tm_sec = 0 , tm_wday = 3 , tm_yday = 75 , tm_isdst = - 1 ) >>>time.strptime( "07/24/2017" , "%m/%d/%Y" ) time.struct_time(tm_year = 2017 , tm_mon = 7 , tm_mday = 24 , tm_hour = 0 , tm_min = 0 , tm_sec = 0 , tm_wday = 0 , tm_yday = 205 , tm_isdst = - 1 ) |
结构化时间 --> %a %b %d %H:%M:%S %Y串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # time.asctime(结构化时间) 如果不传参数,直接返回当前时间的格式化串 res = time.asctime(time.localtime( 1500000000 )) print (res) #输出结果 Fri Jul 14 10 : 40 : 00 2017 res = time.asctime() print (res) # 输出 Fri Jun 9 16 : 32 : 37 2023 # 时间戳 --> %a %b %d %H:%M:%S %Y串 # time.ctime(时间戳) 如果不传参数,直接返回当前时间的格式化串 res = time.ctime() print (res) # 输出 Fri Jun 9 16 : 35 : 45 2023 res = time.ctime( 1500000000 ) print (res) # 输出 Fri Jul 14 10 : 40 : 00 2017 |
二、datatime
1、自定义时间
1 2 | res = datetime.date( 2023 , 6 , 9 ) print (res) # 2023-06-09 |
2、获取本地时间,年月日
1 2 | now_date = datetime.date.today() print (now_date) # 2023-06-09 |
3、获取本地 年月日时分秒
1 2 3 | # now_time = datetime.datetime.today() # 与下面等价 now_time = datetime.datetime.now() print (now_time) # 2023-06-09 16:46:55.305812 |
4、无论是年月日,还是年月日时分秒对象都可以调用以下方法获取针对性的数据
1 2 3 4 5 6 7 8 | now_time = datetime.datetime.today() # 以datetime对象举例 print (now_time.year) # 获取年份2019 print (now_time.month) # 获取月份7 print (now_time.day) # 获取日1 print (now_time.weekday()) # 获取星期(weekday星期是0-6) 0表示周一 print (now_time.isoweekday()) # 获取星期(weekday星期是1-7) 1表示周一 |
5、datetime.timedelta() 方法可以操作时间
如计算7天后的时间
1 2 3 4 5 6 7 8 9 10 11 | # 获得本地日期 年月日 tday = datetime.date.today() # 定义操作时间 day=7 也就是可以对另一个时间对象加7天或者减少7点 tdelta = datetime.timedelta(days = 7 ) # 打印今天的日期 print ( '今天的日期:{}' . format (tday)) #今天的日期:2023-06-09 # 打印七天后的日期 print ( '从今天向后推7天:{}' . format (tday + tdelta)) # 从今天向后推7天:2023-06-16 |
6、小练习 计算举例今年过生日还有多少天
1 2 3 4 5 6 | birthday = datetime.date( 2023 , 12 , 6 ) # 过生日时间 now_date = datetime.date.today() # 今天时间 days = birthday - now_date # 时间差 print ( '我的生日是:{}' . format (birthday)) print ( '今天的日期:{}' . format (now_date)) print ( '距离生日还有{}天' . format (days)) |
3
合集:
python fullstack