In this article, you will see how to use the Python Datetime module to store and manipulate data that is in the form of the date, time, or both date and time. The date class, the time class, and the datetime class are three main classes in the Datetime module. In this article, you will briefly see the functionality of these three classes. So, let’s begin without ado.

Table of Contents

  1. Working with the date class
  2. Working with the time class
  3. Working with the datetime class

Regular Expressions

Working with the date class

The date class stores the day, year, and month for a particular date in the format YYYY-DD-MM. Let’s see how you can create an object of the date class and store a date in it. You will first see how to store today’s date in the date class object.

The following script first imports the date, time, and datetime classes from the Datetime module. It then uses the today() method of the date class which returns the date object containing today’s date. The date class object is then printed on the console:

from datetime import date, time, datetime

todays_date = date.today()
print(todays_date)

In the output, you will see the current date of your system.

You can also convert a string into a date class object. To do so, you need to pass the string and the format of the string to the strptime() method of the Datetime object. Here is an example:

string_date = "March 23, 1992"
str_to_date = datetime.strptime(string_date , "%B %d, %Y").date()
print(type(str_to_date))
print(str_to_date)

The above script converts the string “March 23, 1992” into a date class object. Look at the second parameter of the strptime() method. It consists of the string “%B %d, %Y”. Here %B refers to full month name in string format e.g. March; %d refers to two-digit date e.g 23, and %YYYY refers to a four-digit year i.e. 1992.  While specifying the format for string date to be converted. it is important to follow the sequence in which day, month and year appear in a string.  Finally, the date() method converts the Datetime class object returned by the strptime() method to the date class object. Here is the output of the above script:

<class 'datetime.date'>
1992-03-23

You can see that no matter what format you specify for entering the date, internally the date class object stores the date in the YYYY-MM-DD format.

Let’s see another example of converting a string to a date class object:

string_date2 = "14-Aug-47"
str_to_date2 = datetime.strptime(string_date2 , "%d-%b-%y")
print(type(str_to_date2))
print(str_to_date2)

You can see in the above script that the format has now been updated according to the string. The string contains “14-Aug-47”. The corresponding format is “%d-%b-%” where %b corresponds to shorthand notation for a month e.g Aug, and %YY refers to a two-digit year. Here is the output:

<class 'datetime.datetime'>
2047-08-14 00:00:00

To see the abbreviations used for different types of string formats, check this link.

Finally, you can also create an object of the date class by passing values for the year, month, and day attributes. Look at the following script:

random_date = date(year = 1990, month = 4, day = 10)
print(type(random_date))
print(random_date)

Here is the output of the above script:

<class 'datetime.date'>
1990-04-10

Once you have created a date class object, you can extract the day, month, and year using the day, month, and year attribute as shown in the following script:

print(random_date.day)
print(random_date.month)
print(random_date.year)

Output:

10
4
1990

You can also extract the day of the week from a date via the weekday() method. The days of the week start from 0 and end at 6 where Monday refers to 0 and Sunday corresponds to 6.

print(random_date.weekday())

Output:

1

Finally, you can display a date in the form of a string using the ctime() function. Here is an example:

print(random_date.ctime())

Output:

Tue Apr 10 00:00:00 1990

Working with the time class

The time class specializes in storing the time data in the form of hours, minutes, seconds, milliseconds, and time zone.  The simplest way to create an object of time class is to pass values to the hour, minute, and second attribute of the time class constructor as shown below:

from datetime import time
current_time = time(hour=15, minute=30, second=45)
print(type(current_time))
print(current_time)

The above script creates an object of time class which stores the time “15:30:45” as shown in the following output:

<class 'datetime.time'>
15:30:45

As you did with the date class object, you can also convert the time value in the string to a time class object. Here is an example:

string_time = "20-30-55"
str_to_time = datetime.strptime(string_time, "%H-%M-%S").time()
print(type(str_to_time ))
print(str_to_time)

In the above script, you convert the string “20-35-55” into the time format 20 hours, 35 minutes, and 55 seconds. Notice the format “%H-%M-%S”; here %H refers to hours, %M refers to minutes, and %S refers to seconds.

Furthermore, you can also extract the values of hours, minutes, and seconds from a time class object. To do so, you can use the hour, minute, and second attributes of the time class. Here is an example:

print(str_to_time.hour)
print(str_to_time.minute)
print(str_to_time.second)

Output:

20
30
55

Finally, you can also replace values of hours, minutes, or seconds. Here is an example:

from datetime import time
random_time = time(hour=15, minute=30, second=45)
print(random_time)
random_time = random_time.replace(hour=17)
print(random_time)

The script above first creates an object of the time class with values of 15, 30, and 45 for hours, minutes, and seconds attributes. The object is then printed on the console. Subsequently, the value of the hour attribute is updated to 17 via the replace() method of the time class object, and the new value of the time class object is again printed on the console. Here is the output:

15:30:45
17:30:45

Working with the datetime class

The datetime class is a union of date and time class in terms of functionality. It stores the year, day, month, hour, second, millisecond, and time zone information. The simplest way to create a datetime class object is to pass values for the year, month, day, hour, minute, and second parameters of datetime class constructor as shown in the following script:

random_datetime = datetime(year = 1990, month = 4, day = 10,hour=15, minute=30, second=45 )
print(type(random_datetime))
print(random_datetime)

Output:

<class 'datetime.datetime'>
1990-04-10 15:30:4

You can then access the values for the individual parameters using the parameter name. Here is an example:

print(random_datetime.day)
print(random_datetime.month)
print(random_datetime.year)
print(random_datetime.hour)
print(random_datetime.minute)
print(random_datetime.second)

Output:

10
4
1990
15
30
45

Like date and time classes, you can convert a string to create a datetime object class as shown in the following example:

string_date = "March 23, 1992 - 20-30-55"
str_to_date = datetime.strptime(string_date , "%B %d, %Y - %H-%M-%S")
print(type(str_to_date))
print(str_to_date)

Output:

<class 'datetime.datetime'>
1992-03-23 20:30:55

Finally, you can also get the current system date and time via the now() method of the datetime class object. Here is an example:

from datetime import datetime
current_datetime = datetime.now()
print(type(current_datetime))
print(current_datetime)

Output:

<class 'datetime.datetime'>
2020-12-01 01:01:10.762953