Python DateTime Format

Formatting is a process in which you define the layout of elements for display. Python DateTime formatting is the process of generating a string with how the elements like day, month, year, hour, minutes and seconds be displayed in a string.

For example, displaying the date as DD-MM-YYYY is a format, and displaying the date as MM-DD-YYYY is another format.

In this tutorial, we will learn how to use DateTime object to create a date and time string of required format using datetime.datetime.strftime(). And of course, we will use datetime library in this tutorial.

To format DateTime, we will use strftime() function on the datetime object. strftime() is the short for string format time.

Python DateTime Format
ADVERTISEMENT

Syntax – strftime()

Following is the syntax of strftime() function.

strftime(format)

format is string that describes the specified format to strftime() function for creating date and time string from datetime.datetime object.

strftime() function returns formatted datetime as string based on the given format.

Format Codes

Following is the list of format codes that we use while formatting a string from datetime object using strftime() function.

  • %Y – represents year [0001,…, 2018, 2019,…, 9999]. Four digit string.
  • %m – represents month [01, 02, …, 11, 12]. Two digit string.
  • %d – represents day of month [01, 02, …, 30, 31]. Two digit string.
  • %H – represents hour of day [00, 01, …, 22, 23]. Two digits string.
  • %M – represents minute of hour [00, 01, …, 58, 59]. Two digit string.
  • %S – represents second of minute [00, 01, …, 58, 59]. Two digit string.

These format codes if specified in the format, will be replaced with their corresponding values present in datetime.datetime object. We will learn much about these in the following example programs.

Python DateTime – Format Date & Time – dd/mm/YYYY HH:MM:SS

In the following program, we will learn how to format date as dd/mm/YYYY HH:MM:SS. dd meaning day represented with two digit string, mm is month represented with two digit string, YYYY is year represented with four digit string, HH is hours represented with two digit string, MM is for minutes and SS is for seconds.

The format string would be “%d/%m/%Y %H:%M:%S”. What strfime() function does is that, it replaces %d with the two digit string for the day of datetime.datetime object, replaces %m with two digit representation of month, replaces %Y with the four digit representation of the year of this datetime.datetime object, and so on, as per the format codes.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#date and time format: dd/mm/YYYY H:M:S
format = "%d/%m/%Y %H:%M:%S"
#format datetime using strftime() 
time1 = now.strftime(format)

print("Formatted DateTime:", time1)
Try Online

Output

Formatted DateTime: 18/06/2020 22:47:12

Python DateTime Format – mm/dd/YYYY

In the following program, we will learn how to format date as mm/dd/YYYY. mm is month represented with two digit string, dd meaning day represented with two digit string and YYYY is year represented with four digit string.

The format string would be “%m/%d/%Y”.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#date format: mm/dd/yyyy
format = "%m/%d/%Y"
#format datetime using strftime() 
date1 = now.strftime(format)

print("Formatted Date:", date1)
Try Online

Output

Formatted Date: 06/18/2020

Python DateTime Format – dd/mm/YYYY

In the following program, we will learn how to format date as dd/mm/YYYY. dd meaning day represented with two digit string, mm is month represented with two digit string and YYYY is year represented with four digit string.

The format string would be “%d/%m/%Y”.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#date format: dd/mm/yyyy
format = "%d/%m/%Y"
#format datetime using strftime() 
date1 = now.strftime(format)

print("Formatted Date:", date1)
Try Online

Output

Formatted Date: 18/06/2020

Python DateTime Format – mm-dd-YYYY

In the following program, we will learn how to format date as mm-dd-YYYY. mm is month represented with two digit string, dd meaning day represented with two digit string and YYYY is year represented with four digit string.

The format string would be “%m-%d-%Y”.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#date format: mm-dd-yyyy
format = "%m-%d-%Y"
#format datetime using strftime() 
date1 = now.strftime(format)

print("Formatted Date:", date1)
Try Online

Output

Formatted Date: 06-18-2020

Python DateTime Format – dd-mm-YYYY

In the following program, we will learn how to format date as dd-mm-YYYY. dd meaning day represented with two digit string, mm is month represented with two digit string and YYYY is year represented with four digit string.

The format string would be “%d-%m-%Y”.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#date format: dd-mm-yyyy
format = "%d-%m-%Y"
#format datetime using strftime() 
date1 = now.strftime(format)

print("Formatted Date:", date1)
Try Online

Output

Formatted Date: 18-06-2020

Python DateTime Format – HH:MM:SS

In this example, we will learn how to format time as HH:MM:SS using datetime object. We will use %H for the two digit representation of hours from datetime object, %M for the two digit representation of minutes from datetime object and %S for the two digit representation of seconds from datetime object.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#time format: H:M:S
format = "%H:%M:%S"
#format datetime using strftime() 
time1 = now.strftime(format)

print("Formatted Date:", time1)
Try Online

Output

Formatted Time: 22:44:59

Python DateTime – Format Time – MM:SS

In this example, we will learn how to format time as MM:SS using datetime object. We will use %M for the two digit representation of minutes from datetime object and %S for the two digit representation of seconds from datetime object.

Python Program

from datetime import datetime

#current date and time
now = datetime.now()
#time format: M:S
format = "%M:%S"
#format datetime using strftime() 
time1 = now.strftime(format)

print("Formatted Date:", time1)
Try Online

Output

Formatted Time: 44:18

Conclusion

Concluding this Python Tutorial, we learned how to format datetime object as per our requirement. The presented examples explain the usage of format codes, but are not limited to. You can use the format codes and create your own format for the date and time string.