Arrow

Better dates and times for Python

http://arrow.readthedocs.io/

Installing

pip install arrow

Stdlib datetime


In [ ]:
from datetime import datetime

In [ ]:
local = datetime.now()

In [ ]:
local.strftime('%Y-%m-%d %H:%M:%S')

In [ ]:
local.isoformat()

Arrow


In [ ]:
import arrow

In [ ]:
local = arrow.now()

In [ ]:
local.strftime('%Y-%m-%d %H:%M:%S')

In [ ]:
local.isoformat()

Deltas


In [ ]:
before = arrow.now()

In [ ]:
after = arrow.now()

In [ ]:
after - before

Time zones


In [ ]:
pacific = arrow.utcnow().to('US/Pacific')

In [ ]:
print(pacific.isoformat())

In [ ]:
pacific = arrow.now().to('US/Pacific')

In [ ]:
print(pacific.isoformat())

Time parsing


In [ ]:
arrow.get(1479149518)

In [ ]:
arrow.get('1479149518')

In [ ]:
arrow.get('2016-11-14 18:51:58')

In [ ]:
arrow.get('11/14/2016 18:51:58', 'MM/DD/YYYY HH:mm:ss')

Replace / Shift


In [ ]:
local = arrow.now()
local

In [ ]:
local.replace(second=0, microsecond=0)

In [ ]:
local.replace(seconds=1, microsecond=0)

In [ ]:
local.replace(days=-7)

Humanize


In [ ]:
arrow.now().humanize()

In [ ]:
past = arrow.now().replace(hours=-1)

In [ ]:
past.humanize()

In [ ]:
past = arrow.now().replace(years=-1)

In [ ]:
past.humanize()

In [ ]:
future = arrow.now().replace(hours=2)

In [ ]:
future.humanize()

In [ ]:
future.humanize(locale='es')

Ranges & Spans


In [ ]:
arrow.now().span('hour')

In [ ]:
start = arrow.now()
end = start.replace(minutes=3)
[r for r in arrow.Arrow.range('minute', start, end)]

In [ ]:
[r for r in arrow.Arrow.sparange('minute', start, end)]