Dealing with Date and Time in Python

Jigisha Sata

9 months ago

Python date and time
Python date and time
One of the most important part of data science while performing exploratory data analysis we have to deal with temporal data. Python helps us to deal with the various formats type of date and time gracefully.
Python provides us with a DateTime library which helps us to deal with different dateTime format data.
  • Date Time Representation
  • Date Time Arithmetic
  • Date Time Comparison
Now we will try to understand them one by one.

How do we represent Date Time?

A date can be represented in different formats using different datetime functions. Also, there are some format specifiers which play vital role in displaying the alphabetical parts of a date like name of the month or week day.
We can represent today's date and its various parts of the date as mentioned below.
import datetime

print('Today’s date is: ',
datetime.datetime.today())

today_date = datetime.date.today()

print(today_date)

print('This Year   :', today_date.year)

print ('This Month    :', today_date.month)

print ('Month
Name:',today_date.strftime('%B'))

print ('This Week Day    :', today_date.day)

print ('Week Day
Name:',today_date.strftime('%A'))
The output of the above code is as follows:
Today’s date is:  2020-07-03 11:08:49.474092

2020-07-03

This Year   : 2020

This Month    : 7

Month Name: July

This Week Day    : 3

Week Day Name: Friday

Arithmetic with Date Time
We can also perform some of the arithmetic operations on Date Time. We save various dates into different variables and then apply the relevant mathematical operator to these variables according to our requirements.

import datetime 

#Capture the First Date

day1 = datetime.date(2020,7,3)

print('day1:', day1.ctime())

# Capture the Second Date

day2 = datetime.date(2019, 8, 18)

print('day2:', day2.ctime())

# Find the difference between the dates

print('Number of Days:', day1-day2)

date_today  = datetime.date.today() 

# Create a delta of Four Days 

no_of_days = datetime.timedelta(days=4)


# Use Delta for Past Date

before_four_days = date_today -
no_of_days 

print('Before Four Days:',
before_four_days) 

# Use Delta for future Date

after_four_days = date_today +
no_of_days 

print('After Four Days:',
after_four_days)
The output of the above code is as follows:
day1: Fri Jul  3 00:00:00 2020

day2: Sun Aug 18 00:00:00 2019

Number of Days: 320 days, 0:00:00

Before Four Days: 2020-06-29

After Four Days: 2020-07-07

How do we compare Date Time?

Sometimes we have to compare date and time. It is done by using logical operators. But we should always be careful while comparing the rights parts of the dates with each other.
Let’s take an example here we take the future and past dates and compare them using the python if clause along with logical operators.

import datetime 

date_today  = datetime.date.today() 

print('Today is: ',date_today)

# Create a delta of Four Days 

no_of_days = datetime.timedelta(days=4) 

# Use Delta for Past Date

before_four_days = date_today - no_of_days 

print('Before Four Days:', before_four_days) 

after_four_days =  date_today + no_of_days

date1 = datetime.date(2018,4,4)

print( 'date1:',date1)

if date1 == before_four_days :

    print ('Same Dates')

if date_today > date1:

    print ('Past Date')

if date1 < after_four_days:

    print ('Future Date')
The output of the above code is as follows

Today is: 2020-07-03

Before Four Days: 2020-06-29

date1: 2018-04-04

Past Date

Future Date
I hope you enjoyed reading this article and finally, you came to know about Dealing with Date and Time in Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review