How to work with date and time in Python

This post explains how to use Arrow Python library for creating, manipulating, formatting and converting dates, times and timestamps.

Why Arrow

Python's standard library and some other low-level modules have near-complete date, time and timezone functionality, but lacks in ease of use usability, while Arrow implements and updates the datetime type, plugging gaps in functionality and providing an intelligent module API that supports many common creation scenarios. Let's have a look to most common datetime operations in Python using Arrow.

How to install Arrow
Arrow can be installed via pip.
 
pip install Arrow
     

Get current UTC time in Python
   
import arrow

current_time = arrow.utcnow()
print(current_time)

 
Output
   
2022-02-12T04:38:28.614718+00:00
 
Convert current time to different time zones in Python
   
import arrow

current_time_asia_kolkata = arrow.utcnow().to("Asia/Kolkata")
current_time_US_Pacific = arrow.utcnow().to("US/Pacific")

print(current_time_asia_kolkata)
print(current_time_US_Pacific)
    
 
Output
   
2022-02-12T10:14:08.222019+05:30
2022-02-11T20:44:08.562828-08:00
 
Shift current timestamp to future or past time in Python
   
import arrow

current_time = arrow.utcnow()
print(current_time)

# time after 2 hour in future
ct_future = current_time.shift(hours=2)
print(ct_future)

# time after 2 hour 30 mins
ct_future_m = current_time.shift(hours=2, minutes=30)
print(ct_future_m)

# time before 2 hours in past
ct_past = current_time.shift(hours=-2)
print(ct_past)

# time before 2 hours 30 mins in past
ct_past_m = current_time.shift(hours=-2, minutes=-30)
print(ct_past_m)
 
Output
   
2022-02-12T04:55:18.543544+00:00
2022-02-12T06:55:18.543544+00:00
2022-02-12T07:25:18.543544+00:00
2022-02-12T02:55:18.543544+00:00
2022-02-12T02:25:18.543544+00:00
 
Replace hour, minute, etc of the timestamp in Python
   
import arrow

current_time = arrow.utcnow()
print(current_time)

# replace hour
t1 = current_time.replace(hour=14)
print(t1)

# replace day
t2 = current_time.replace(day=10)
print(t2)

# replace minute
t3 = current_time.replace(minute=30)
print(t3)
 
Output
   
2022-02-12T05:01:22.856956+00:00
2022-02-12T14:01:22.856956+00:00
2022-02-10T05:01:22.856956+00:00
2022-02-12T05:30:22.856956+00:00
 
Parse from a string format to datetime object in Python
   
import arrow

# parse from a string format with timezone to Arrow timestamp object
timestamp = arrow.get("2020-06-05 16:34:44 Asia/Kolkata", "YYYY-MM-DD HH:mm:ss ZZZ")
print(type(timestamp), timestamp)

# with list of formats to try parsing the string timestamp
ts1 = arrow.get("2020-06-05 16:34:44", ["MM/DD/YYYY", "YYYY-MM-DD HH:mm:ss"])
print(type(ts1), ts1)

ts2 = arrow.get("06/06/2020 ", ["MM/DD/YYYY", "YYYY-MM-DD HH:mm:ss"])
print(type(ts2), ts2)
 
Output
   
<class 'arrow.arrow.Arrow'> 2020-06-05T16:34:44+05:30
<class 'arrow.arrow.Arrow'> 2020-06-05T16:34:44+00:00
<class 'arrow.arrow.Arrow'> 2020-06-06T00:00:00+00:00
 
Search date in string in Python
   
import arrow

sample_str = "We won many tournaments in May 2016"
dt = arrow.get(sample_str, "MMMM YYYY")
print(dt)
 
Output
   
2016-05-01T00:00:00+00:00
 

Category: Python