7

I'm using the arrow module to handle datetime objects in Python. If I get current time like this:

now = arrow.now()

...how do I increment it by one day?

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
serverpunk
  • 10,665
  • 15
  • 61
  • 95

3 Answers3

13

Update as of 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

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

  • 0.14.5 (2019-08-09) [CHANGE] Removed deprecated replace shift functionality. Users looking to pass plural properties to the replace function to shift values should use shift instead.
  • 0.9.0 (2016-11-27) [FIX] Separate replace & shift functions

Increment the day

now.replace(days=1)

Decrement the day

now.replace(days=-1)

I highly recommend the docs.

chishaku
  • 4,577
  • 3
  • 25
  • 33
  • According to the *highly recommended docs* `replace` will just alter the attribute and `shift` will move it relatively. There is no `days` attribute for `replace`, but there is `day` which changes the date to given value. Why is this the accepted answer when `shift` is the correct solution? – nik Jul 18 '20 at 14:40
  • @nik surely you know the answer to your own question. Things change. But just in case you're familiar with the docs but not the changelog, you can find when the api changed here: https://arrow.readthedocs.io/en/stable/releases.html. – chishaku Jul 18 '20 at 15:05
  • 1
    Thanks for updating--was helpful for a quick reminder this evening :-) – Jason R Stevens CFA Sep 18 '20 at 02:48
5

The docs state that shift is to be used for adding offsets:

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.

In any case, I think e.g. now.shift(hours=-1) is much clearer than now.replace.

  • *Curiously*, `days`, `hours`, `minutes` are no longer known to `replace`. The way to go **now** is `now.shift(days=1)` as you have correctly answered. – nik Jul 18 '20 at 14:46
-1

See documentation

now = arrow.now()
oneDayFromNow = now.replace(days+=1)
hlongmore
  • 1,603
  • 24
  • 28
Ben
  • 1