简体   繁体   English

如何使用箭头增加日期?

[英]How to increment a date using Arrow?

I'm using the arrow module to handle datetime objects in Python.我正在使用箭头模块来处理 Python 中的datetime对象。 If I get current time like this:如果我得到这样的当前时间:

now = arrow.now()

...how do I increment it by one day? ...我如何将它增加一天?

Update as of 2020-07-28更新至 2020-07-28

Increment the day增加一天

now.shift(days=1)

Decrement the day减少一天

now.shift(days=-1)

Original Answer原答案

DEPRECATED as of 2019-08-09自 2019-08-09 起弃用

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

  • 0.14.5 (2019-08-09) [CHANGE] Removed deprecated replace shift functionality. 0.14.5 (2019-08-09) [CHANGE] 删除了已弃用的替换班次功能。 Users looking to pass plural properties to the replace function to shift values should use shift instead.希望将多个属性传递给替换函数以移动值的用户应改用 shift。
  • 0.9.0 (2016-11-27) [FIX] Separate replace & shift functions 0.9.0 (2016-11-27) [FIX] 单独的替换和移位功能

Increment the day增加一天

now.replace(days=1)

Decrement the day减少一天

now.replace(days=-1)

I highly recommend the docs.我强烈推荐文档。

The docs state that shift is to be used for adding offsets:文档指出shift将用于添加偏移量:

now.shift(days=1)

The replace method with arguments like days , hours , minutes , etc. seems to work just as shift does, though replace also has day , hour , minute , etc. arguments that replace the value in given field with the provided value.带有dayshoursminutes等参数的replace方法似乎和 shift 一样工作,尽管 replace 也有dayhourminute等参数,这些参数用提供的值替换给定字段中的值。

In any case, I think eg now.shift(hours=-1) is much clearer than now.replace .无论如何,我认为例如now.shift(hours=-1)now.replace清楚得多。

See documentation查看文档

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM