【发布时间】:2016-10-10 09:50:48
【问题描述】:
【问题讨论】:
标签: python datetime arrow-python
【问题讨论】:
标签: python datetime arrow-python
2020 年 7 月 28 日更新
增加天数
now.shift(days=1)
递减天数
now.shift(days=-1)
原答案
自 2019 年 8 月 9 日起已弃用
https://arrow.readthedocs.io/en/stable/releases.html
增加天数
now.replace(days=1)
递减天数
now.replace(days=-1)
【讨论】:
replace 只会改变属性,shift 会相对移动它。 replace 没有 days 属性,但有 day 将日期更改为给定值。当shift 是正确的解决方案时,为什么这是公认的答案?
now = arrow.now()
oneDayFromNow = now.replace(days+=1)
【讨论】:
docs 声明 shift 将用于添加偏移量:
now.shift(days=1)
带有 days、hours、minutes 等参数的 replace 方法似乎与 shift 一样有效,尽管 replace 也有 day、hour、minute 等. 用提供的值替换给定字段中的值的参数。
无论如何,我认为例如now.shift(hours=-1) 比 now.replace 清晰得多。
【讨论】:
days、hours、minutes 不再为 replace 所知。 现在要走的路是now.shift(days=1),因为您已正确回答。