bertday
bertday

Reputation: 10971

How to increment a date using Arrow?

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?

Upvotes: 7

Views: 8676

Answers (3)

chishaku
chishaku

Reputation: 4643

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.

Upvotes: 13

Ben
Ben

Reputation: 1

See documentation

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

Upvotes: -1

Eric Woudenberg
Eric Woudenberg

Reputation: 69

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.

Upvotes: 5

Related Questions