Master Arrow in Python: Convert UTC, Local, and datetime Seamlessly
This article provides a step‑by‑step guide on using the Python Arrow library to obtain UTC and local times, format dates, shift time values, parse arbitrary timestamps, and convert Arrow objects to native datetime, date, and time types, complete with code examples and result screenshots.
Outline
People often ask how to learn a knowledge point; this outline shows that summarizing and comparing concepts, following a clear learning logic, prevents feeling overwhelmed.
1. Arrow and local time usage
1) Get current UTC time
utc = arrow.utcnow()
print(utc)2) Get current local time
local = utc.now()
print(local)
# or
local = utc.to("local")
print(local)3) Get timestamp of local time
local.timestamp4) Format Arrow time
local.format("YYYY-MM-DD hh:mm:ss ZZ")
local.format("YYYY-MM-DD hh:mm:ss")5) Shift Arrow time forward or backward
print(local.shift(years=1))
print(local.shift(years=-1))
print(local.shift(months=1))
print(local.shift(months=-1))2. Arrow and arbitrary time usage
1) Create Arrow from numeric components
arrow.get(2020, 8, 1, 14, 55, 55)
arrow.get(2020, 8, 1)2) Parse Arrow from a formatted string
arrow.get('2020,08-01 14:58:55', 'YYYY,MM-DD hh:mm:ss')
arrow.get('John was born in May 2000', 'MMMM YYYY')3) Parse Arrow from a timestamp
arrow.get(1596261535)3. Converting Arrow to native datetime types
1) Arrow to datetime
arrow.utcnow().now()
arrow.utcnow().now().datetime
arrow.utcnow().to("local").naive2) Arrow to date
arrow.utcnow().to("local")
arrow.utcnow().to("local").date()3) Arrow to time
arrow.utcnow().to("local")
arrow.utcnow().to("local").time()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
