Python 3 – Date & Time
Python 3 has a built-in module called “datetime” for working with dates and times. In this article, we will learn how to use this module to work with date and time in Python.
Getting the Current Date and Time
To get the current date and time, we use the “datetime” module’s “datetime” class, which provides attributes for working with year, month, day, hour, minute, second, and microsecond. Here’s an example:
import datetime
now = datetime.datetime.now()
print("Current date and time: ")
print(now)
Output:
Current date and time:
2022-01-01 20:27:06.387000
In this example, we imported the “datetime” module and created an instance of the “datetime” class called “now,” which holds the current date and time. We printed the variable “now,” which displays the current date and time in the format of yyyy-mm-dd hh:mm:ss.microseconds.
Formatting Date and Time Output
The “strftime()” method is used to format the output of date and time. This method allows us to format the date and time output as per our needs. Here is an example:
import datetime
now = datetime.datetime.now()
print("Formatted date and time: ")
print(now.strftime("%Y-%m-%d %H:%M:%S"))
Output:
Formatted date and time:
2022-01-01 20:45:29
In this example, we first created an instance of the “datetime” class called “now,” as before. Then we used the “strftime()” method to format the output of the date and time. This method takes a string as an argument, and the string contains formatting codes that specify how the date and time should be displayed.
Here, “%Y” represents the year, “%m” represents the month, “%d” represents the day, “%H” represents the hour, “%M” represents the minute, and “%S” represents the second.
Creating a Date Object
In Python, we can create a date object using the “date()” class, which is part of the “datetime” module. Here is an example:
import datetime
d = datetime.date(2022, 1, 1)
print(d)
Output:
2022-01-01
In this example, we created an instance of the “date” class called “d,” which holds the date 2022-01-01.
Formatting Date Output
We can use the “strftime()” method to format the output of a date object. Here is an example:
import datetime
d = datetime.date(2022, 1, 1)
print(d.strftime("%d-%B-%Y"))
Output:
01-January-2022
In this example, we created an instance of the “date” class called “d,” as before. Then we used the “strftime()” method to format the output of the date. This method takes a string as an argument containing formatting codes that specify how the date should be displayed. In this example, “%d” represents the day, “%B” represents the month name, and “%Y” represents the year.
Creating a Time Object
We can create a time object using the “time()” class, which is part of the “datetime” module. Here is an example:
import datetime
t = datetime.time(20, 55, 30)
print(t)
Output:
20:55:30
In this example, we created an instance of the “time” class called “t,” which holds the time 20:55:30.
Formatting Time Output
We can use the “strftime()” method to format the output of a time object. Here is an example:
import datetime
t = datetime.time(20, 55, 30)
print(t.strftime("%H:%M:%S"))
Output:
20:55:30
In this example, we created an instance of the “time” class called “t,” as before. Then we used the “strftime()” method to format the output of the time. This method takes a string as an argument containing formatting codes that specify how the time should be displayed.
Combining Date and Time Objects
We can combine a date and a time object to create a datetime object using the “datetime()” class, which is part of the “datetime” module. Here is an example:
import datetime
d = datetime.date(2022, 1, 1)
t = datetime.time(20, 55, 30)
dt = datetime.datetime.combine(d, t)
print(dt)
Output:
2022-01-01 20:55:30
In this example, we created instances of the “date” and “time” classes called “d” and “t,” respectively. Then we used the “combine()” method of the “datetime” class to combine the “date” and “time” instances into a “datetime” instance called “dt.”
Manipulating Date and Time Objects
We can manipulate date and time objects using various methods provided by the “datetime” module. Here are some examples:
Add Days to a Date
We can add days to a date object using the “timedelta()” method. Here is an example:
import datetime
d = datetime.date(2022, 1, 1)
td = datetime.timedelta(days=7)
nd = d + td
print(nd)
Output:
2022-01-08
In this example, we created an instance of the “date” class called “d,” as before. Then we used the “timedelta()” method to add seven days to the date object. Finally, we added the timedelta object to the date object using the “+” operator to get the new date “nd.”
Subtract Days from a Date
We can subtract days from a date object using the “timedelta()” method. Here is an example:
import datetime
d = datetime.date(2022, 1, 8)
td = datetime.timedelta(days=7)
nd = d - td
print(nd)
Output:
2022-01-01
In this example, we created an instance of the “date” class called “d,” which holds the date 2022-01-08. Then we used the “timedelta()” method to subtract seven days from the date object. Finally, we subtracted the timedelta object from the date object using the “-” operator to get the new date “nd.”
Difference between Two Dates
We can find out the difference between two date objects by subtracting one from the other. Here is an example:
import datetime
d1 = datetime.date(2022, 1, 1)
d2 = datetime.date(2022, 1, 8)
diff = d2 - d1
print(diff.days)
Output:
7
In this example, we created two instances of the “date” class called “d1” and “d2,” respectively. Then we subtracted the date object “d1” from date object “d2” to get the difference between the two dates. We used the “.days” attribute to get the difference in days.
Difference between Two Times
We can find out the difference between two time objects by subtracting one from the other. Here is an example:
import datetime
t1 = datetime.time(20, 47, 0)
t2 = datetime.time(23, 55, 30)
diff = datetime.datetime.combine(datetime.date.today(), t2) - datetime.datetime.combine(datetime.date.today(), t1)
print(diff)
Output:
3:08:30
In this example, we created two instances of the “time” class called “t1” and “t2,” respectively. Then we combined the time objects with the current date using the “datetime.combine()” method to create “datetime” objects. Finally, we subtracted the “datetime” object “t1” from “datetime” object “t2” to get the difference between the two times.
Conclusion
In this article, we learned how to work with dates and times in Python 3. We covered how to get the current date and time, format the output of date and time, create a date object, format the output of a date object, create a time object, format the output of a time object, combine date and time objects to create a datetime object, and manipulate date and time objects. These concepts are fundamental to Python programming, and understanding them will help you create powerful applications in Python.