python - 使用日期进行计算,无需 import dateutil

标签 python python-2.7

from datetime import datetime
from dateutil import relativedelta
date1 = datetime.strptime(str('2011-08-15 12:00:00'), '%Y-%m-%d %H:%M:%S')
date2 = datetime.strptime(str('2012-02-15'), '%Y-%m-%d')
r = relativedelta.relativedelta(date2, date1)
r.months

上面的代码对我有用,但我不想导入 dateutil。有人给我一个不循环的例子吗?

我想将两个日期相减,并且想知道这两个日期在整个月份中的月份差异。

最佳答案

看来这篇文章有帮助:How do I find the time difference between two datetime objects in python?

只需对两个日期时间对象进行减法,即可在差异中获得所需的详细信息。

关于python - 使用日期进行计算,无需 import dateutil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38658521/

相关文章:

python - 为什么 pandas groupby 在数据框中添加额外的 (groupby_info) 列以及如何删除它们?

python - 通过符号将实值 numpy 数组转换为二进制数组

python - 可选的结束符号和使用正则表达式捕获的几个单词

python - 将youtube视频下载到Django网站上的用户计算机

python - 查找错误 : Resource 'corpora/stopwords' not found

python - 按创建/修改日期移动文件然后使用 Python 移动

python - 如果向前的前 4 位数字等于向后的最后 4 位数字,则检查数字

python - Cython 中的并行性不起作用

python - 带有 Python 错误结果的 OpenCV (cv2) 中的 undistortPoints

Python 将日期时间设置为 UTC 时区