python3 中时间戳、时间、日期的转换和加减操作
作者:甄超锋 时间:2023-12-31 17:41:36
1.当前时间戳转换为指定格式的日期
# -*- coding: utf-8 -*-
# @Time : 2019/5/31 10:56
# @Author : 甄超锋
# @Email : 4535@sohu.com
# @File : test.py
# @Software: PyCharm
import datetime
import time
# 使用time
timeStamp = time.time() # 1559286774.2953627
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime) # 2019-05-31 15:12:54
# 使用datetime
timeStamp = time.time() # 1559286774.2953627
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime) # 2019-05-31 07:12:54
2.把字符串类型的日期转换为时间戳
# -*- coding: utf-8 -*-
# @Time : 2019/5/31 10:56
# @Author : 甄超锋
# @Email : 4535@sohu.com
# @File : test.py
# @Software: PyCharm
import time
# 字符类型的时间1
tss1 = '2019-05-31 15:12:54'
# 转为时间数组
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
print(timeArray)
# timeArray可以调用tm_year等
print(timeArray.tm_year) # 2019
# 字符类型的时间2
tss2 = "Fri Jun 21 13:22:37 +0800 2019"
timeArray = time.strptime(tss2, "%a %b %d %H:%M:%S %z %Y")
# timeArray可以调用tm_year等
print(timeArray.tm_year) # 2019
# 转为时间戳
timeStamp = int(time.mktime(timeArray))
print(timeStamp) # 1559286774
3.更改str类型日期的显示格式
# -*- coding: utf-8 -*-
# @Time : 2019/5/31 10:56
# @Author : 甄超锋
# @Email : 4535@sohu.com
# @File : test.py
# @Software: PyCharm
import time
import datetime
tss2 = "2019-05-31 15:12:54"
# 转为数组
timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
# 转为其它显示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(otherStyleTime) # 2019/05/31 15:12:54
tss3 = "2019/05/31 15:12:54"
timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime) # 2019-05-31 15:12:54
tss4 = "2019/05/31 15:12:54"
otherStyleTime = datetime.datetime.strptime(tss4, "%Y/%m/%d %H:%M:%S").strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime) # 2019-05-31 15:12:54
4.日期的加减
两个日期的加减,计算间隔多少天。
# -*- coding: utf-8 -*-
# @Time : 2019/5/31 10:56
# @Author : 甄超锋
# @Email : 4535@sohu.com
# @File : test.py
# @Software: PyCharm
import datetime
d1 = datetime.datetime.strptime('2019-05-31 15:12:54', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime('2019-05-22 15:12:54', '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print(delta.days) # 9 间隔9天
今天3天后的日期。
# -*- coding: utf-8 -*-
# @Time : 2019/5/31 10:56
# @Author : 甄超锋
# @Email : 4535@sohu.com
# @File : test.py
# @Software: PyCharm
import datetime
now = datetime.datetime.now()
delta = datetime.timedelta(days=3)
n_days = now + delta
print(n_days.strftime('%Y-%m-%d %H:%M:%S')) # 2019-06-03 15:38:32
今天3天前的日期。
# -*- coding: utf-8 -*-
# @Time : 2019/5/31 10:56
# @Author : 甄超锋
# @Email : 4535@sohu.com
# @File : test.py
# @Software: PyCharm
import datetime
now = datetime.datetime.now()
delta = datetime.timedelta(days=3)
n_days = now - delta
print(n_days.strftime('%Y-%m-%d %H:%M:%S')) # 2019-05-28 15:39:33
在datetime模块中有timedelta类,这个类的对象用于表示一个时间间隔,比如两个日期或者时间的差别。
构造方法:
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
所有的参数都有默认值0,这些参数可以是int或float,正的或负的。
可以通过timedelta.days、tiemdelta.seconds等获取相应的时间值。
timedelta类的实例,支持加、减、乘、除等操作,所得的结果也是timedelta类的实例。
由于tiemdelta最高只能减去天数 所以减月份要用另一个方法下面的是现在的时间减去3个月 得到3个月前的日期
import datetime
import dateutil.relativedelta
now_date_time = datetime.datetime.now()
month_date = now_date_time + dateutil.relativedelta.relativedelta(months=-3)
日期时间格式化(带毫秒)
import datetime
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')) # 2019-01-28 11:09:01.529864
来源:https://blog.csdn.net/Mr_Zhen/article/details/90717584
标签:python3,时间戳,时间,日期,加减
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
对numpy中向量式三目运算符详解
2023-06-28 02:14:55
使用PyV8在Python爬虫中执行js代码
2022-05-09 14:33:36
python实现在线翻译功能
2023-06-02 22:12:38
新手程序员编程必不可少的工具
2023-10-24 08:16:25
![](https://img.aspxhome.com/file/2023/1/109541_0s.jpg)
Bootstrap Metronic完全响应式管理模板之菜单栏学习笔记
2023-08-17 10:58:20
![](https://img.aspxhome.com/file/2023/7/56047_0s.jpg)
python读取txt文件并取其某一列数据的示例
2022-09-25 01:58:09
Pytest+request+Allure实现接口自动化框架
2023-08-12 17:29:33
![](https://img.aspxhome.com/file/2023/7/64297_0s.jpg)
解决Golang中goroutine执行速度的问题
2023-08-25 20:12:12
如何改良你的CSS代码编写结构
2008-09-29 16:03:00
![](https://img.aspxhome.com/file/UploadPic/up/2008092916133772.gif)
百分百弹窗
2013-08-07 07:41:06
将mater库中的系统存储过程批量生成*.sql文件 通用且非常实用
2012-06-06 20:03:43
mysql中使用sql命令将时间戳解析成datetime类型存入
2024-01-22 00:00:25
![](https://img.aspxhome.com/file/2023/7/107417_0s.png)
python 字典 setdefault()和get()方法比较详解
2022-12-11 01:28:41
![](https://img.aspxhome.com/file/2023/3/67753_0s.png)
python+playwright微软自动化工具的使用
2022-10-18 08:52:29
![](https://img.aspxhome.com/file/2023/1/112531_0s.png)
教你利用Selenium+python自动化来解决pip使用异常
2022-11-17 18:49:08
![](https://img.aspxhome.com/file/2023/0/110200_0s.png)
oracle行转列方法集合汇总(推荐!)
2024-01-12 19:42:14
![](https://img.aspxhome.com/file/2023/1/65671_0s.png)
Python3爬虫发送请求的知识点实例
2023-04-17 19:16:32
![](https://img.aspxhome.com/file/2023/9/124369_0s.png)
js计算页面刷新的次数
2024-05-09 10:36:47
Django contenttypes 框架详解(小结)
2023-11-13 14:39:47
![](https://img.aspxhome.com/file/2023/0/90910_0s.png)
使用python对泰坦尼克号幸存者进行数据分析与预测
2022-09-07 09:29:42
![](https://img.aspxhome.com/file/2023/6/131726_0s.png)