Arrow is a Python module for working with date and time. It offers a sensible and human-friendly approach to creating, manipulating, formatting and converting dates, times and timestamps. It allows easy creation of date and time instances with timezone awareness.
Installation
The arrow module is installed with the following command:
pip install arrow
Features
- User friendly.
- Timezone-aware and UTC by default.
- Timezone conversion.
- Time frame ranging from microsecond to year.
- Easy to use.
- Formats and parses strings automatically.
- Supports a growing list of contributed locales.
Getting UTC (Universal Time Coordinated) time.
In order to get current UTC time we use utcnow() method.
Python3
# importing arrow module
import arrow
# getting UTC time
utc_time = arrow.utcnow()
# printing the current UTC time
print('Current UTC Time is =', utc_time)
Output :
Current UTC Time is = 2020-02-28T18:06:39.228924+00:00
Getting Indian time.
In order to get current regional(Indian) time we use now() method.
Python3
# importing arrow module
import arrow
# getting current indian time
ind_time = arrow.now('Asia/Calcutta')
# printing the time
print('Current India Time =', ind_time)
Output :
Current India Time = 2020-02-28T23:40:07.112695+05:30
Parsing string to date
In order to parse the string into date format we use get() method.
Python3
# importing arrow module
import arrow
# date in string format
s ='2020-02-02 12:30:45'
# parsing string into date
date = arrow.get(s, 'YYYY-MM-DD HH:mm:ss')
# printing the date
print(date)
Output :
2020-02-02T12:30:45+00:00
Unix time
Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, that is the time 00:00:00 UTC on 1 January 1970, minus leap seconds.
- timestamp() method is used to get unix time.
- fromtimestamp() method used to convert the Unix time back to the arrow date object.
Python3
# importing arrow module
import arrow
# getting current utc time
utc = arrow.utcnow()
# printing the unix time
print(utc)
# getting unix time
unix_time = utc.timestamp
# printing unix time
print(unix_time)
# converting unix time into arrow date object
date = arrow.Arrow.fromtimestamp(unix_time)
# printing arrow dateobject
print(date)
Output :
2020-03-04T13:33:15.041536+00:00
1583328795
2020-03-04T19:03:15+05:30
Arrow instance from datetime
An instance of the arrow module can also be created from the DateTime module. Consider the below example for a better understanding of the topic.
Python3
# importing arrow module
import arrow
# importing datetime from datetime module
from datetime import datetime
# getting current time using datetime module
dt = datetime.now()
# creating arrow instance from datetime instance
arrow_dt = arrow.Arrow.fromdate(dt)
# printing datetime instance
print(dt)
# printing arrow instance
print(arrow_dt)
Output :
2020-03-04 19:16:04.317690
2020-03-04T00:00:00+00:00
Properties for getting individual datetime objects
if you want to get any object as an individual, here are some properties that can be used.
Python3
#import arrow module
import arrow
#Call datetime functions that return properties
a = arrow.utcnow()
print(a.time())
print(a.date())
#Get any datetime value
print(a.year)
Output :
datetime.time(19, 16, 04, 317690)
datetime.date(2020, 3, 4)
2020
Replace and Shift property
if you want to replace or shift any object as an individual, here are some properties that can be used.
Python3
#import arrow module
import arrow
# getting current utc time
a = arrow.utcnow()
# printing the unix time without alteration
print("without alteration: ",a)
# replacing only the hours to 5 and minutes to 30
b = a.replace(hour=5, minute=30)
print("with hours and minutes replaced: ",b)
# shifting forward in weeks
c = a.shift(weeks=+3)
print("with weeks shifted 3 forward: ",c)
# replacing only the timezone
d = a.replace(tzinfo='US/Pacific')
print("with timezone replaced: ",d)
Output :
without alteration: 2020-03-04T13:33:15.041536+00:00
with hours and minutes replaced: 2020-03-04T05:30:15.041536+00:00
with weeks shifted 3 forward: 2020-03-25T13:33:15.041536+00:00
with timezone replaced: 2020-03-04T13:33:15.041536-07:00
Humanized format
All the above properties and function outputs are more of a computer format but what if you wanted it to be more of a human form? for example: "an hour ago" or "2 hours ago",here are some properties that can be used to achieve humanized format.
Python3
#import arrow module
import arrow
#Humanize to past
apast = arrow.utcnow().shift(hours=-1)
print(apast.humanize())
#humanize to future
present = arrow.utcnow()
afuture = present.shift(hours=3)
print(afuture.humanize(present))
#Indicate a specific time granularity
afuture = present.shift(minutes=73)
print(afuture.humanize(present, granularity="minute"))
print(afuture.humanize(present, granularity=["hour", "minute"]))
Output :
'an hour ago'
'in 3 hours'
'in 73 minutes'
'in an hour and 13 minutes'
Similar Reads
matplotlib.pyplot.arrow() in Python Matplotlib is a very powerful plotting library Python, widely used for data visualization and statistical analysis. One of its useful functions is arrow(), which lets you add arrows to your plots to highlight or point out specific data points or trends. This article explains how to use the arrow() f
2 min read
Matplotlib.axes.Axes.arrow() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
Python OpenCV | cv2.arrowedLine() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.arrowedLine() method is used to draw arrow segment pointing from the start point to the end point. Syntax: cv2.arrowedLine(image, start_point, end_point, color, thickness, line_type, shift, tipLength)Param
3 min read
turtle.down() method in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.down() The turtle.down() method is used to pull back the pen down on the scr
1 min read
Python Graph Tools Module The graph-tools module is a Python package designed for handling directed and undirected graphs, as well as complex networks. It emphasizes performance and provides a wide range of graph algorithms and data structures. It was initially developed for networking researchers who conduct experiments in
2 min read
Making an arrow with VPython VPython makes it easy to create navigable 3D displays and animations, even for those with limited programming experience. Because it is based on Python, it also has much to offer for experienced programmers and researchers. VPython allows users to create objects such as spheres and cones in 3D space
4 min read
Matplotlib.patches.ArrowStyle class in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.patches.ArrowStyle The matplotlib.patches.ArrowStyle class is a container cla
2 min read
turtle.backward() method in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.backward() The turtle.backward() method is used to move the turtle backward
1 min read
Learn Python Basics âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in
9 min read
Python Dictionary items() method items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p
2 min read