datetime Module in Python
datetime Module in Python
Python provides a module named datetime to work on dates and times in the script if needed. Python has a number of modules with a number of useful classes, functions and data structures that can be used in the current script. To use a module, it has to be included in the script and it is done by using the import statement.
datetime Class of datetime Module in Python
One of the classes that the datetime module has is datetime. It has functions that work on date and time both. One of such functions is now() which returns current date and time to the python script in which it is called.
Get Current Date and Time
# Python program to illustrate now() function of datetime class of datetime module import datetime as dt var_datetime = dt.datetime.now() print(var_datetime)
Output:
2022-01-27 17:28:32.111179
Create datetime Object
# Python program to illustrate creation of datetime object import datetime as dt var_datetime = dt.datetime(2021, 10, 15) print(var_datetime) var_datetime = dt.datetime(2021, 10, 15, 23, 6, 25, 111) print(var_datetime)
Output:
2021-10-15 00:00:00 2021-10-15 23:06:25.000111
Use datetime Object
# Python program to illustrate using a datetime object import datetime as dt var_datetime = dt.datetime.now() print("year:", var_datetime.year) print("month:", var_datetime.month) print("day:", var_datetime.day) print("hour:", var_datetime.hour) print("minute:", var_datetime.minute) print("second:", var_datetime.second) print("microsecond:", var_datetime.microsecond)
Output:
year: 2022 month: 1 day: 27 hour: 17 minute: 39 second: 44 microsecond: 869649
date Class of datetime Module
One of the classes that the datetime module has is date. It has functions that work on date only. One of such functions is today() which returns current date to the python script in which it is called.
Get Current Date
# Python program to exemplify today() method import datetime as dt d = dt.date.today() print(d)
Output:
2022-02-01
Create date Object
# Python program to exemplify date() method import datetime as dt d = dt.date(2020,10,12) print(d)
Output:
2020-10-12
Use date Object
# Python program to exemplify date() method import datetime as dt d = dt.date(2020, 10, 12) print("Year:", d.year) print("Month:", d.month) print("Day:", d.day)
Output:
Year: 2020 Month: 10 Day: 12
time Class of datetime Module
One of the classes that the datetime module has is time. It has functions that work on time only. It is used when the programmer needs to work only on time related matters.
Create time Object
# Python program to exemplify time() method import datetime as dt t1 = dt.time(23, 19, 55) print(t1) t2 = dt.time(10, 48, 47, 100) print(t2)
Output:
23:19:55 10:48:47.000100
Use time Object
# Python program to exemplify time() method import datetime as dt t1 = dt.time(23, 19, 55, 101) print("Hour:", t1.hour) print("Minute:", t1.minute) print("second:", t1.second) print("Microsecond:", t1.microsecond)
Hour: 23 Minute: 19 second: 55 Microsecond: 101
timedelta Class of datetime Module
A timedelta object is created by the difference of two datetime objects or two date objects. It can also be created by using the timedelta method defined in the datetime module.
Example1:
# Python program to exemplify datetime() method import datetime as dt x1 = dt.datetime(year=2020, month=10, day=30, hour=20, minute=35, second=40) x2 = dt.datetime(year=2017, month=8, day=6, hour=3, minute=17, second=35) td1 = x1 - x2 y1 = dt.date(year = 2018, month =10, day = 11) y2 = dt.date(year = 2017, month = 9, day = 3) td2 = y1 - y2 print(td1) print("type of td1:",type(td1)) print(td2) print("type of td2:",type(td2))
Output:
1181 days, 17:18:05 type of td1: <class 'datetime.timedelta'> 403 days, 0:00:00 type of td2: <class 'datetime.timedelta'>
The strftime() Method
datetime, date and time classes of the datetime module has a method called strftime() method defined in them. This function is used to format datetime, date and time objects to make them more readable.
Example1:
# Python program to exemplify strftime() method import datetime as dt dt1 = dt.datetime(2020, 10, 23, 12, 45, 10) print("Formatting a datetime object:") print(dt1.strftime("%d/%m/%Y")) print(dt1.strftime("%d %B,%Y")) print(dt1.strftime("%d %B,%Y %H:%M:%S")) d = dt.date(2020, 10, 23) print("\nFormatting a date object:") print(d.strftime("%d/%m/%Y")) print(d.strftime("%d %B,%Y")) t = dt.time(12, 45, 10) print("\nFormatting a time object:") print(t.strftime("%H:%M:%S")) print(t.strftime("%H hours %M minutes %S seconds"))
Output:
Formatting a datetime object: 23/10/2020 23 October,2020 23 October,2020 12:45:10 Formatting a date object: 23/10/2020 23 October,2020 Formatting a time object: 12:45:10 12 hours 45 minutes 10 seconds