Arrow: Better Dates & Times for Python
Arrow is a Python library that offers a sensible, human-friendly approach to dates, times, and timestamps. It aims to be a drop-in replacement for the `datetime` module, providing a more convenient API for creating, manipulating, formatting, and converting dates and times, including robust timezone handling and humanization. The current version is 1.4.0, and it maintains an active release cadence, with updates typically occurring every few months.
Warnings
- breaking Arrow dropped support for Python 3.6 and 3.7 starting with version 1.3.0. Users on these Python versions must upgrade their Python environment to 3.8 or newer to use Arrow 1.3.0+.
- gotcha Prior to version 1.1.1, explicit `tzinfo` arguments to `arrow.get()` might not have been properly respected in all cases, potentially leading to incorrect timezone awareness for parsed dates.
- gotcha Arrow objects are not direct subclasses of Python's built-in `datetime.datetime` objects. While they offer a similar interface, direct compatibility with libraries expecting `datetime.datetime` might require explicit conversion using the `.datetime` property.
- gotcha Just like standard `datetime`, `arrow` can create timezone-naive objects if no timezone information is provided during creation or parsing. Operations involving naive and aware objects can lead to `TypeError` or incorrect results.
Install
-
pip install arrow
Imports
- arrow
import arrow
Quickstart
import arrow
# Get current UTC time
now_utc = arrow.utcnow()
print(f"Current UTC: {now_utc}")
# Get current local time
now_local = arrow.now()
print(f"Current Local: {now_local}")
# Parse a string into an Arrow object
dt_string = '2023-10-27T10:30:00-05:00'
dt_obj = arrow.get(dt_string)
print(f"Parsed datetime: {dt_obj}")
# Format an Arrow object
formatted_dt = dt_obj.format('YYYY-MM-DD HH:mm:ss ZZ')
print(f"Formatted datetime: {formatted_dt}")
# Shift an Arrow object
tomorrow = now_local.shift(days=+1)
yesterday = now_local.shift(days=-1)
print(f"Tomorrow: {tomorrow}")
print(f"Yesterday: {yesterday}")
# Humanize an Arrow object
humanized_diff = tomorrow.humanize(now_local)
print(f"Tomorrow relative to now: {humanized_diff}")