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
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read