All Courses

# Dealing with Date and Time in Python

Jigisha Sata

2 years ago

• Introduction
• How do we represent Date Time?
• How do we compare Date Time?

## Introduction

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?

The date can be represented in different formats using different date-time functions. Also, there are certain aspects of formatting that play an important role in determining the alphabetical parts of a day such as the name of the month or day of the week.
We can represent today's date and its various parts of the date as mentioned below.
``````import datetime

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

date = datetime.date.today()

print(date)

print('Current Year  :', date.year)

print ('Current Month :', date.month)

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

print ('Current Week Day :', date.day)

print ('Week Day Name :',date.strftime('%A'))``````
Output
``````Today"s date and time is :  2021-07-07 09:37:26.512851

2021-07-07

Current Year  : 2021

Current Month : 7

Current Month Name: July

Current Week Day : 7

Week Day Name : Wednesday``````
We may also perform other mathematical operations on Date and time. Fold different dates with different variations and use the right mathematical operator for these variables according to our needs.
``````import datetime

d1 = datetime.date(2020,7,3) # First Date

print('Day 1 :', d1.ctime())

d2 = datetime.date(2019, 8, 18) # Second Date

print('Day 2 :', d2.ctime())

print('Number of Days:', d1-d2) # difference between the dates

today = datetime.date.today()

no_of_days = datetime.timedelta(days=3)

#  Delta for Past Date

before_three_days = today - no_of_days

print('Before three Days:', before_three_days)

#  Delta for future Date

after_three_days = today + no_of_days

print('After three Days:',after_three_days)``````
Output
``````Day 1 : Fri Jul  3 00:00:00 2020

Day 2 : Sun Aug 18 00:00:00 2019

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

Before three Days: 2021-07-04

After three Days: 2021-07-10``````

## 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

today = datetime.date.today()

print('Today is: ',today)

no_of_days = datetime.timedelta(days=3)

before_three_days = today - no_of_days

print('Before three Days:', before_three_days)

after_three_days = today + no_of_days

d1 = datetime.date(2019,5,5)

print( 'Date 1 :',d1)

if d1 == before_three_days :

print ('Same Dates')

if today > d1:

print ('Past Date')

if d1 < after_three_days:

print ('Future Date')``````
Output
``````Today is:  2021-07-07

Before three Days: 2021-07-04

Date 1 : 2019-05-05

Past Date

Future Date``````

Like the Blog, then Share it with your friends and colleagues to make this AI community stronger.