Python has a built-in module called datetime to work with dates and times. To use the module, it must be imported in the current script.
One of the class defined in datetime module is datetime class. The datetime class contains datetime(),datetime(),datetime() method which is used to get current time and date. This class is used when it is required to work with dates and times both.
In the below example, datetime module is imported in the current script and renamed as dt. The datetime(),datetime(),datetime() of datetime class is used to get current time and date. The output contains year, month, day, hour, minute, second, and microsecond.
import datetime as dt x = dt.datetime.datetime(),datetime(),datetime() print(x)
2019-11-19 17:47:49.843822
As discussed earlier, a datetime object contains information about year, month, day, hour, minute, second, and microsecond. It can be created by user by passing arguments in datetime class constructor. Please see the example below for syntax.
import datetime as dt x = dt.datetime(2019, 10, 15) print(x) y = dt.datetime(2019, 10, 15, 10, 15, 25, 111) print(y)
2019-10-15 00:00:00 2019-10-15 10:15:25.000111
A datetime object can be used to find out year, month, day, hour, minute, second, and microsecond separately for a particular date as shown in the below example.
import datetime as dt x = dt.datetime(2019, 10, 15, 10, 15, 25, 111) print("year:", x.year) print("month:", x.month) print("day:", x.day) print("hour:", x.hour) print("minute:", x.minute) print("second:", x.second) print("microsecond:", x.microsecond)
year: 2019 month: 10 day: 15 hour: 10 minute: 15 second: 25 microsecond: 111
Another class defined in datetime module is date class. The date class contains today() method which can be used to get current date. This class is used when it is required to work with dates only.
The today() method of date class is used to get current date. The output contains year, month, and day.
import datetime as dt x = dt.date.today() print(x)
2019-11-19
As discussed earlier, a date object contains information about year, month, and day. A date object can be created by user by passing arguments in date class constructor. Please see the example below for syntax.
import datetime as dt x = dt.date(2019, 10, 15) print(x)
2019-10-15
A date object can be used to find out year, month, and day separately for a particular date as shown in the below example.
import datetime as dt x = dt.date(2019, 10, 15) print("year:", x.year) print("month:", x.month) print("day:", x.day)
year: 2019 month: 10 day: 15
Another class defined in datetime module is time class. The time class used when it is required to work with time only.
A time object contains information about hour, minute, second, and microsecond. It can be created by user by passing arguments in time class constructor. Please see the example below for syntax.
import datetime as dt x = dt.time(10, 15, 25) print(x) y = dt.time(9, 18, 27, 111) print(y)
10:15:25 09:18:27.000111
A time object can be used to find out hour, minute, second, and microsecond separately for a particular time as shown in the below example.
import datetime as dt x = dt.time(10, 15, 25, 111) print("hour:", x.hour) print("minute:", x.minute) print("second:", x.second) print("microsecond:", x.microsecond)
hour: 10 minute: 15 second: 25 microsecond: 111
Difference of two datetime objects or date objects creates a timedelta object.
import datetime as dt x1 = dt.datetime(year = 2019, month =11, day = 10, hour = 10, minute = 15, second = 20) x2 = dt.datetime(year = 2018, month = 6, day = 5, hour = 5, minute = 10, second = 15) t1 = x1 - x2 y1 = dt.date(year = 2019, month =11, day = 10) y2 = dt.date(year = 2018, month = 6, day = 5) t2 = y1 - y2 print(t1) print("type of t1:",type(t1)) print(t2) print("type of t2:",type(t2))
523 days, 5:05:05 type of t1: <class 'datetime.timedelta'> 523 days, 0:00:00 type of t2: <class 'datetime.timedelta'>
A timedelta object can also be created using keywords as shown in the below example. Difference of two timedelta objects is again a timedelta object. Along with this, it can be converted into seconds.
import datetime as dt x = dt.timedelta(weeks = 10, days = 10, hours = 10, minutes = 15, seconds = 20) y = dt.timedelta(weeks = 10) t = x - y print(t) print(t.seconds)
10 days, 10:15:20 36920
The strftime method is defined under datetime, date and time classes of datetime module. It is used to format datetime, date or time objects in more readable format.
import datetime as dt x = dt.datetime(2019, 10, 5, 10, 15, 20) print("Formatting a datetime object:") print(x.strftime("%d/%m/%Y")) print(x.strftime("%d %B,%Y")) print(x.strftime("%d %B,%Y %H:%M:%S")) y = dt.date(2019, 10, 5) print("\nFormatting a date object:") print(y.strftime("%d/%m/%Y")) print(y.strftime("%d %B,%Y")) z = dt.time(10, 15, 20) print("\nFormatting a time object:") print(z.strftime("%H:%M:%S")) print(z.strftime("%H hours %M minutes %S seconds"))
Formatting a datetime object: 05/10/2019 05 October,2019 05 October,2019 10:15:20 Formatting a date object: 05/10/2019 05 October,2019 Formatting a time object: 10:15:20 10 hours 15 minutes 20 seconds
The strftime() formats discussed in above example have following interpretation.
To learn more about strftime(), please visit - .
The strptime method is defined under datetime class of datetime module. It is used to convert string into a datetime object. To convert a string into datetime object, it need to be in certain format.
import datetime as dt MyString = "25 Oct, 2019" x = dt.datetime.strptime(MyString, "%d %b, %Y") print(x) MyString = "24/9/2018" x = dt.datetime.strptime(MyString, "%d/%m/%Y") print(x) MyString = "23/8/2017 10:15:20" x = dt.datetime.strptime(MyString, "%d/%m/%Y %H:%M:%S") print(x) MyString = "22/7/2016 9 hr 18 min 27 sec" x = dt.datetime.strptime(MyString, "%d/%m/%Y %H hr %M min %S sec") print(x)
2019-10-25 00:00:00 2018-09-24 00:00:00 2017-08-23 10:15:20 2016-07-22 09:18:27
To learn more about strptime(), please visit - Next Page.