Python Date and Time, Import datetime module in Python, Date Time Representation, Date Time Arithmetic, and Date Time Comparison.
Python Date and Time
Python has a built-in module named datetime to work with dates and times.
Date (Year, Month, and Day)
Time (Hour, Minute, Second, MicroSecond)
Python’s DateTime Classes
1.datetime — allows us to manipulate dates and times together (month, day, year, hour, second, microsecond).
2. date— allows us to manipulate dates only (month, day, year).
3. time — you probably guessed it; this class allows us to manipulate time only (hour, minute, second, microsecond).
4. timedelta — used for measuring duration, the difference between two dates or times.
5. tzinfo — used for dealing with time zones. We won’t be covering this one in this tutorial.
We have different Date & Time scenarios.
1. Date Time Representation
2. Date Time Arithmetic
3. Date Time Comparison
1. Date Time Representation
Date and its various parts are represented by using different datetime functions.
Example:
import datetime
print (‘The Date Today is :’, datetime.datetime.today())
date_today = datetime.date.today()
print (date_today)
print (‘This Year :’, date_today.year)
print (‘This Month :’, date_today.month)
print (‘Month Name:’,date_today.strftime(‘%B’))
print (‘This Week Day :’, date_today.day)
print (‘Week Day Name:’,date_today.strftime(‘%A’))
2. 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
day1 = datetime.date(2018, 2, 12)
print (‘day1:’, day1.ctime())
# Capture the Second Date
day2 = datetime.date(2017, 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)
3. 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.
Timedelta function is mainly used to calculate the duration between two dates and times.
Example:
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’)
Python Complete Tutorial
Python Video Tutorial
Python Programming Syllabus
Python Programming Quiz
Python Interview Questions for Fresher
1. Introduction to Python Programming Language
2. Download and Install Python
4. Python Keywords and Identifiers
9. Python Conditional Statements
11. Python Branching Statements
14. Python Data Structures – Lists
15. Python Data Structures – Sets
16. Python Data Structures – Tuples
17. Python Data Structures – Dictionaries
18. Python User Defined Functions
Follow me on social media: