Python中时间datetime的处理与转换用法总结
作者:gavenyeah 时间:2022-05-03 01:23:01
python中日期类datetime功能比较强大,使用起来很方便,把常用的两种用法总结如下:
from datetime import datetime
from datetime import timedelta
# 日期格式转换为字符串
NOW = datetime.now()
TODAY = NOW.strftime("%Y%m%d")
YESTERDAY = (NOW -timedelta(days=1)).strftime("%Y%m%d")
# 字符串转换为日期
d1 = str(20180301)
d2 = str(20180226)
print type(d1)
d1 = datetime.strptime(d1, '%Y%m%d')
d2 = datetime.strptime(d2, '%Y%m%d')
# 计算 d1与d2之间的差值
print (d1 - d2).days
来源:https://blog.csdn.net/y999666/article/details/80079609
标签:python,datetime,处理,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
将Reporting services的RDL文件拷贝到另外一台机器时报Data at the root level is invalid的解决方法
2012-07-11 15:33:45
JavaScript事件委托技术实例分析
2023-07-01 01:18:23
Python Pygame中精灵和碰撞检测详解
2021-01-31 05:45:27
perl的格式化(Format)报表输出实现代码
2022-02-10 09:43:47
MySQL中一条update语句是如何执行的
2024-01-16 23:57:43
![](https://img.aspxhome.com/file/2023/6/77216_0s.jpg)
Python连接es之es更新操作示例详解
2022-11-21 06:11:48
实现一个完整的Node.js RESTful API的示例
2024-05-13 09:35:35
python变量赋值方法(可变与不可变)
2021-02-14 08:51:55
![](https://img.aspxhome.com/file/2023/9/69559_0s.jpg)
asp如何准确获知对方来访问的时间和URL?
2010-07-07 12:25:00
Mysql事务操作失败如何解决
2024-01-27 15:17:50
Vue ElementUI之Form表单验证遇到的问题
2023-07-02 16:56:54
![](https://img.aspxhome.com/file/2023/1/139841_0s.png)
一篇文章带你了解python正则表达式的正确用法
2023-01-05 19:20:30
![](https://img.aspxhome.com/file/2023/6/133806_0s.png)
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2024-05-05 09:08:16
oracle 存储过程加密的方法
2009-03-06 10:58:00
python 贪心算法的实现
2023-01-25 08:18:50
![](https://img.aspxhome.com/file/2023/5/133725_0s.png)
Python3.7将普通图片(png)转换为SVG图片格式(网站logo图标)动起来
2021-09-26 00:05:53
![](https://img.aspxhome.com/file/2023/3/115723_0s.png)
OpenCV MediaPipe实现颜值打分功能
2022-06-19 08:22:16
![](https://img.aspxhome.com/file/2023/9/114749_0s.jpg)
MySQL存储引擎MyISAM与InnoDB区别总结整理
2024-01-15 13:18:22
![](https://img.aspxhome.com/file/2023/7/67507_0s.png)
python实现图片转字符画
2021-02-13 22:58:33
![](https://img.aspxhome.com/file/2023/8/92098_0s.jpg)
python re模块匹配贪婪和非贪婪模式详解
2023-04-19 01:44:16