Python Arrow Module
Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating, formatting and converting dates, times and timestamps. It implements and updates the datetime type, plugging gaps in functionality and providing an intelligent module API.
Installation
pip install arrow
Basic Usage
Creating Arrow Objects
import arrow
# Current time (UTC)
utc = arrow.utcnow()
# Current time (Local)
local = arrow.now()
# From a string
dt = arrow.get('2023-05-11 21:23:58', 'YYYY-MM-DD HH:mm:ss')
# From a timestamp
ts = arrow.get(1367900664)
Properties
Arrow objects have standard datetime properties.
t = arrow.now()
print(t.year, t.month, t.day, t.hour)
Manipulation
Shifting (Time Arithmetic)
Use the shift method to shift the time.
import arrow
now = arrow.now()
future = now.shift(hours=1)
past = now.shift(weeks=-1)
Formatting
now = arrow.now()
print(now.format('YYYY-MM-DD HH:mm:ss ZZ'))
Humanization
Arrow provides powerful humanization features.
past = arrow.now().shift(hours=-1)
print(past.humanize())
# Output: an hour ago
# Different locale
print(past.humanize(locale='ko_kr'))
# Output: 한시간 전
Timezone Conversion
utc = arrow.utcnow()
pacific = utc.to('US/Pacific')