python中time,datetime模块学习
2020-03-28 本文已影响0人
卅清
一、time模块
时间表示方法:
时间戳类型:time.time()
![](https://img.haomeiwen.com/i20542283/b329b29c8fd8b564.png)
数组的类型即struct_time:time.localtime()
![](https://img.haomeiwen.com/i20542283/220aca79b05b6163.png)
时间戳与struct_time的相互转换:
时间戳-->struct_time:gmtime()
![](https://img.haomeiwen.com/i20542283/f64a94a588034495.png)
struct_time-->时间戳:mktime()
![](https://img.haomeiwen.com/i20542283/ba3a5af22fb7df25.png)
struct_time类型与字符型的相互转换:
struct_time 转换成字符串:strftime()
![](https://img.haomeiwen.com/i20542283/73cf42dc1fc238ae.png)
字符串转换成struct_time:strptime()
![](https://img.haomeiwen.com/i20542283/1ad307258de26158.png)
二、datetime模块
datetime.datetime:表示日期时间。
![](https://img.haomeiwen.com/i20542283/670329f78f0f5972.png)
通time模块一样,也提供了strftime与strptime进行datetime类型与字符串的转化
![](https://img.haomeiwen.com/i20542283/14c6c3a66ce3f5c8.png)
![](https://img.haomeiwen.com/i20542283/f0941f14b66df9d5.png)
datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
三、time与datetime之间的互相转换
时间戳--->datetime:
![](https://img.haomeiwen.com/i20542283/62cfea220a76605c.png)
datetime-->struct_time:
![](https://img.haomeiwen.com/i20542283/a77e270ca3332dc5.png)
struct_time-->datetime:
可通过中间状态string完成,也可让struct_time,转化为时间戳,再变为datetime
![](https://img.haomeiwen.com/i20542283/8e11776bc36e1b93.png)
![](https://img.haomeiwen.com/i20542283/9cf3749c96c5d38a.png)
四、指定日期和时间的加减
timedelta(weeks=0, days=0, hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0)
![](https://img.haomeiwen.com/i20542283/089c54e81827cafb.png)