Often in data science we need analysis which is based on temporal values. Python can handle the various formats of date and time gracefully. The datetime library
provides necessary methods and functions to handle the following scenarios.
- Date Time Representation
- Date Time Arithmetic
- Date Time Comparison
We will study them one by one.Date Time Representation
A date and its various parts are represented by using different datetime functions. Also, there are format specifiers which play a role in displaying the alphabetical parts of a date like name of the month or week day. The following code shows today's date and various parts of the date.
import datetime print 'The Date Today 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')
When we execute the above code, it produces the following result.
The Date Today is : 2021-02-27 15:38:35.835000 2021-02-27 This Year : 2021 This Month : 02 Month Name: February This Week Day : 27 Week Day Name: Saturday
Date Time Arithmetic
For calculations involving dates we store the various dates into variables and apply the relevant mathematical operator to these variables.
import datetime #Capture the First Date d1 = datetime.date(2021, 2, 27) print 'day1:', d1.ctime() # Capture the Second Date d2 = datetime.date(2019, 6, 18) print 'day2:', d2.ctime() # Find the difference between the dates print 'Number of Days:', day1-day2 today_date = datetime.date.today() # Create a delta of Four Days no_days = datetime.timedelta(days=4) # Use Delta for Past Date before_four_days = today_date - no_days print 'Before Four Days:', before_four_days # Use Delta for future Date after_four_days = today_date + no_days print 'After Four Days:', after_four_days
When we execute the above code, it produces the following result.
day1: Sat Feb 27 00:00:00 2021 day2: Fri Jun 06 00:00:00 2019 Number of Days: 178 days, 0:00:00 Before Four Days: 2021-02-23 After Four Days: 2021-03-03
Date Time Comparison
Date and time are compared using logical operators. But we must be careful in comparing the right parts of the dates with each other. In the below examples
we take the future and past dates and compare them using the python if clause along with logical operators.import datetime today_date = datetime.date.today() print 'Today is: ', today_date # Create a delta of Four Days no_days = datetime.timedelta(days=4) # Use Delta for Past Date before_four_days = today_date - no_days print 'Before Four Days:', before_four_days after_four_days = today_date + no_days d1 = datetime.date(2021,2,27) print 'date1:',today_date if d1 == before_four_days : print 'Same Dates' if d1 > date1: print 'Past Date' if d1 < after_four_days: print 'Future Date'
When we execute the above code, it produces the following result.
Today is: 2021-02-27 Before Four Days: 2021-02-23 date1: 2018-04-04 Past Date Future Date