Python日期的加减等操作的示例
作者:XZC.Log 时间:2021-10-06 14:28:14
本文介绍了Python日期的加减等操作的示例,分享给大家,也给自己留个笔记
1. 日期输出格式化
所有日期、时间的api都在datetime模块内。
1. datetime => string
now = datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')
#输出2012-03-05 16:26:23.870105
strftime是datetime类的实例方法。
2. string => datetime
t_str = '2012-03-05 16:26:23'
d = datetime.datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
strptime是datetime类的静态方法。
2. 日期比较操作
在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类的实例。比如:
year = timedelta(days=365)
ten_years = year *10
nine_years = ten_years - year
同时,date、time和datetime类也支持与timedelta的加、减运算。
datetime1 = datetime2 +/- timedelta
timedelta = datetime1 - datetime2
这样,可以很方便的实现一些功能。
1. 两个日期相差多少天。
d1 = datetime.datetime.strptime('2012-03-05 17:41:20', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime('2012-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print delta.days
输出:3
2. 今天的n天后的日期。
now = datetime.datetime.now()
delta = datetime.timedelta(days=3)
n_days = now + delta
print n_days.strftime('%Y-%m-%d %H:%M:%S')
输出:2012-03-08 17:44:50
#coding=utf-8
import datetime
now=datetime.datetime.now()
print now
#将日期转化为字符串 datetime => string
print now.strftime('%Y-%m-%d %H:%M:%S')
t_str = '2012-03-05 16:26:23'
#将字符串转换为日期 string => datetime
d=datetime.datetime.strptime(t_str,'%Y-%m-%d %H:%M:%S')
print d
#在datetime模块中有timedelta类,这个类的对象用于表示一个时间间隔,比如两个日#期或者时间的差别。
#计算两个日期的间隔
d1 = datetime.datetime.strptime('2012-03-05 17:41:20', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime('2012-03-02 17:41:20', '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print delta.days
print delta
#今天的n天后的日期。
now=datetime.datetime.now()
delta=datetime.timedelta(days=3)
n_days=now+delta
print n_days.strftime('%Y-%m-%d %H:%M:%S')
来源:http://www.blogjava.net/xzclog/archive/2017/08/14/432743.html?utm_source=tuicool&utm_medium=referral
标签:Python,日期,加减
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Python OS模块常用文件/目录函数详解
2023-07-04 06:30:06
![](https://img.aspxhome.com/file/2023/9/71509_0s.png)
Python中HMAC加密算法的应用
2021-07-29 15:55:18
python3的数据类型及数据类型转换实例详解
2022-06-30 11:24:45
![](https://img.aspxhome.com/file/2023/7/65447_0s.png)
Django使用HTTP协议向服务器传参方式小结
2023-03-08 11:31:20
![](https://img.aspxhome.com/file/2023/6/71336_0s.png)
Access为后台数据库的网站统计系统
2008-11-16 17:34:00
python 爬虫基本使用——统计杭电oj题目正确率并排序
2021-11-25 17:16:35
golang组件swagger生成接口文档实践示例
2023-09-19 11:29:39
![](https://img.aspxhome.com/file/2023/3/100793_0s.png)
Python ArcPy实现批量拼接长时间序列栅格图像
2021-09-07 18:48:53
![](https://img.aspxhome.com/file/2023/0/81800_0s.jpg)
tensorflow下的图片标准化函数per_image_standardization用法
2022-10-10 13:54:06
![](https://img.aspxhome.com/file/2023/0/85370_0s.jpg)
考虑SQL Server安全时所应注意的几个方面
2009-01-04 13:57:00
python读取txt文件并取其某一列数据的示例
2022-09-25 01:58:09
在 Python 应用中使用 MongoDB的方法
2023-09-13 21:53:11
ASP、PHP与javascript根据时段切换CSS皮肤的代码
2008-09-01 17:26:00
python实现简单的计算器功能
2021-02-08 03:38:54
![](https://img.aspxhome.com/file/2023/5/77605_0s.jpg)
asp产生不重复的随机数
2008-06-03 13:29:00
详解Python中的List
2023-11-20 05:37:39
![](https://img.aspxhome.com/file/2023/7/63877_0s.png)
ORACLE11g随RHEL5系统自动启动与关闭的设置方法
2009-08-31 12:43:00
JavaScript缓动库
2009-05-25 12:50:00
Phar反序列化超详细介绍
2023-06-05 07:06:02
![](https://img.aspxhome.com/file/2023/8/55368_0s.png)
Javascript 获取css属性
2009-05-31 16:49:00