【问题标题】:How to increment a date using Arrow?如何使用箭头增加日期?
【发布时间】:2016-10-10 09:50:48
【问题描述】:

我正在使用 arrow 模块来处理 Python 中的 datetime 对象。如果我得到这样的当前时间:

now = arrow.now()

...如何将其增加一天?

【问题讨论】:

    标签: python datetime arrow-python


    【解决方案1】:

    2020 年 7 月 28 日更新

    增加天数

    now.shift(days=1)
    

    递减天数

    now.shift(days=-1)
    

    原答案

    自 2019 年 8 月 9 日起已弃用

    https://arrow.readthedocs.io/en/stable/releases.html

    • 0.14.5 (2019-08-09) [更改] 删除了已弃用的替换班次功能。希望将多个属性传递给 replace 函数以移位值的用户应该使用 shift 代替。
    • 0.9.0 (2016-11-27) [修复] 单独的替换和移位功能

    增加天数

    now.replace(days=1)
    

    递减天数

    now.replace(days=-1)
    

    I highly recommend the docs.

    【讨论】:

    • 根据强烈推荐的文档replace 只会改变属性,shift 会相对移动它。 replace 没有 days 属性,但有 day 将日期更改为给定值。当shift 是正确的解决方案时,为什么这是公认的答案?
    • @nik 你肯定知道自己问题的答案。事情会改变的。但以防万一您熟悉文档但不熟悉变更日志,您可以在此处找到 api 何时更改:arrow.readthedocs.io/en/stable/releases.html
    • 感谢您的更新——对今晚的快速提醒很有帮助 :-)
    【解决方案2】:

    documentation

    now = arrow.now()
    oneDayFromNow = now.replace(days+=1)
    

    【讨论】:

      【解决方案3】:

      docs 声明 shift 将用于添加偏移量:

      now.shift(days=1)

      带有 dayshoursminutes 等参数的 replace 方法似乎与 shift 一样有效,尽管 replace 也有 dayhourminute 等. 用提供的值替换给定字段中的值的参数。

      无论如何,我认为例如now.shift(hours=-1)now.replace 清晰得多。

      【讨论】:

      • 奇怪地dayshoursminutes 不再为 replace 所知。 现在要走的路是now.shift(days=1),因为您已正确回答。
      猜你喜欢
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多