Python comes with a variety of useful objects that can be used out of the box. Date objects are examples of such objects. Date types are difficult to manipulate from scratch, due to the complexity of dates and times. However, Python date objects make it extremely easy to convert dates into the desirable string formats.
Date formatting is one of the most important tasks that you will face as a programmer. Different regions around the world have different ways of representing dates/times, therefore your goal as a programmer is to present the date values in a way that is readable to the users.
For example, you may need to represent a date value numerically like "02-23-2018". On the flip side, you may need to write the same date value in a longer textual format like "Feb 23, 2018". In another scenario, you may want to extract the month in string format from a numerically formated date value.
In this article, we will study different types of date objects along with their functionalities.
The datetime Module
datetime module, as you probably guessed, contains methods that can be used to work with date and time values. To use this module, we first import it via the
import statement as follows:
We can represent time values using the
time class. The attributes for the
time class include the hour, minute, second and microsecond.
The arguments for the
time class are optional. Although if you don't specify any argument you will get back a time of 0, which is unlikely to be what you need most of the time.
For example, to initialize a time object with a value of 1 hour, 10 minutes, 20 seconds and 13 microseconds, we can run the following command:
t = datetime.time(1, 10, 20, 13)
To see the time, let's use the
You may need to see either the hour, minute, second, or microsecond only, here is how you can do so:
The minutes, seconds and microseconds for the above time can be retrieved as follows:
print('Minutes:', t.minute) print('Seconds:', t.second) print('Microsecond:', t.microsecond)
Minutes: 10 Seconds: 20 Microseconds: 13
The values for the calendar date can be represented via the
date class. The instances will have attributes for year, month, and day.
Let us call the
today method to see today's date:
import datetime today = datetime.date.today() print(today)
The code will return the date for today, therefore the output you see will depend on the day you run the above script.
Now let's call the
ctime method to print the date in another format:
ctime: Sat Sep 15 00:00:00 2018
ctime method uses a longer date-time format than the examples we saw before. This method is primarily used for converting Unix-time (the number of seconds since Jan. 1st, 1970) to a string format.
And here is how we can display the year, the month, and the day using the
print('Year:', today.year) print('Month:', today.month) print('Day :', today.day)
Year: 2018 Month: 9 Day : 15
Converting Dates to Strings with strftime
Now that you know how to create Date and Time objects, let us learn how to format them into more readable strings.
To achieve this, we will be using the
strftime method. This method helps us convert date objects into readable strings. It takes two parameters, as shown in the following syntax:
The first parameter is the format string, while the second parameter is the time to be formatted, which is optional.
This method can also be used on a
datetime object directly, as shown in the following example:
import datetime x = datetime.datetime(2018, 9, 15) print(x.strftime("%b %d %Y %H:%M:%S"))
Sep 15 2018 00:00:00
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
We have used the following character strings to format the date:
%b: Returns the first three characters of the month name. In our example, it returned "Sep"
%d: Returns day of the month, from 1 to 31. In our example, it returned "15".
%Y: Returns the year in four-digit format. In our example, it returned "2018".
%H: Returns the hour. In our example, it returned "00".
%M: Returns the minute, from 00 to 59. In our example, it returned "00".
%S: Returns the second, from 00 to 59. In our example, it returned "00".
We did not pass a time, hence the values for time are all "00". The following example shows how the time can be formatted as well:
import datetime x = datetime.datetime(2018, 9, 15, 12, 45, 35) print(x.strftime("%b %d %Y %H:%M:%S"))
Sep 15 2018 12:45:35
The Complete Character Code List
Other than the character strings given above, the
strftime method takes several other directives for formatting date values:
%a: Returns the first three characters of the weekday, e.g. Wed.
%A: Returns the full name of the weekday, e.g. Wednesday.
%B: Returns the full name of the month, e.g. September.
%w: Returns the weekday as a number, from 0 to 6, with Sunday being 0.
%m: Returns the month as a number, from 01 to 12.
%p: Returns AM/PM for time.
%y: Returns the year in two-digit format, that is, without the century. For example, "18" instead of "2018".
%f: Returns microsecond from 000000 to 999999.
%Z: Returns the timezone.
%z: Returns UTC offset.
%j: Returns the number of the day in the year, from 001 to 366.
%W: Returns the week number of the year, from 00 to 53, with Monday being counted as the first day of the week.
%U: Returns the week number of the year, from 00 to 53, with Sunday counted as the first day of each week.
%c: Returns the local date and time version.
%x: Returns the local version of date.
%X: Returns the local version of time.
Consider the following example:
import datetime x = datetime.datetime(2018, 9, 15) print(x.strftime('%b/%d/%Y'))
And here is how you can get the month only:
Let us display the year:
In this example we have used the format code
%Y. Notice that the
Y is in uppercase. Now write it in lowercase:
This time, the century has been omitted. As you can see, with these formatting codes you can represent the date-time in just about any form that you'd like.
Converting Strings to Dates with strptime
strftime method helped us convert date objects into more readable strings. The
strptime method does the opposite, that is, it takes strings and converts them into date objects that Python can understand.
Here is the syntax for the method:
string parameter is the value in string format that we want to convert into date format. The
format parameter is the directive specifying the format to be taken by the date after the conversion.
For example, let's say we need to convert the string "9/15/18" into a
Let's first import the
datetime module. We will use the
from keyword in order to be able to reference the specific module functions without the dot format:
from datetime import datetime
We can then define the date in the form of a string:
str = '9/15/18'
Python will not be able to understand the above string as a datetime until we convert it to an actual
datetime object. We can successfully do so by calling the
Execute the following command to convert the string:
date_object = datetime.strptime(str, '%m/%d/%y')
Let's now call the
As you can see, the conversion was successful!
You can see that the forward slash "/" has been used to separate the various elements of the string. This tells the
strptime method what format our date is in, which in our case "/" is used as a separator.
But what if the day/month/year was separated by a "-"? Here is how you'd handle that:
from datetime import datetime str = '9-15-18' date_object = datetime.strptime(str, '%m-%d-%y') print(date_object)
And again, thanks to the format specifier the
strptime method was able to parse our date and convert it to a date object.
In this article, we studied how to format dates in Python. We saw how the
datetime module in Python can be used for the manipulation of date and time values. The module contains a number of classes that can be used for this purpose. For example, the
time class is used to represent time values while the
date class is used to represent calendar date values.