How to Display the Date in Python


In this article, we show how to display the date in Python.

We can display the date by importing the datetime module.

The datetime module does not need to be installed, because it comes with the python package that you downloaded.

We'll show how to get the current date and how to format the date to any format that you desire.

How to Get the Current Date

To get the current date with the datetime function, we can use the statement, This gives us the current date.

This is shown below.

I wrote this article on April 1, 2017 at 6:06PM, so this is the results that I obtained when running this code.

The first parameter (2017) is the year. The second parameter (4) is the month (April). The third parameter (1) is the day (the 1st of the month). The foruth parameter (18) is the hour. The fifth parameter (6) is the minute. The sixth parameter 26 is the second.

To display these parameters separately, this is how we would do it.

This is how you can show the numerical values for each of these parameters: year, month, day, hour, minute, and second.

However, this can be converted to many different other formats.

How to Convert Dates to Other Formats

The full table to convert dates to many other different formats is shown below.

strftime directive Meaning
%Y Year with 4 digits, such as '2017'
%y Year with 2 digits, such as '17
%m Month as a decimal number, '01' to '12'
%B Full month name, as in 'December'
%b Abbreviated month name, as in 'Dec'
%d Day of the month, '01' to '31'
%j Day of the year '001' to '366'
%w Day of the week, '0' (Sunday) to '6' (Saturday)
%A Full weekday name, as in 'Monday'
%a Abbreviated weekday name, as in 'Mon'
%H Hour (24-hour clock), '00' to '23'
%I Hour (12-hour clock), '01' to '12'
%M Minute, '00' to '59'
%S Second, '00' to '59'
%p 'AM' or 'PM'

So, to display dates in any formats, we have some examples, shown below.

So, this shows how you can format the date to many other formats.

You can use the chart above to get to any format that you want.

Realize also that you don't have to have the current date. You can create any date that you want.

For example, say, we want to create the date, June 10th, 2017.

And this is all that is required to display and format dates in Python.

Related Resources

HTML Comment Box is loading comments...