Formatting and parsing dates and times is a common task when working with Python. The datetime
module provides several functions and classes for formatting and parsing dates and times, as well as performing arithmetic with dates and times.
Datetime Functions
The datetime
module provides several functions for formatting dates and times as strings:
datetime.strftime()
: Formats adatetime
object as a string using a specified format.time.strftime()
: Formats atime
object as a string using a specified format.date.strftime()
: Formats adate
object as a string using a specified format.
The strftime()
function uses format codes to specify the format of the output string.
Strftime() Functions
Some common format codes are:
%Y
: The full year (4 digits)%m
: The month (2 digits)%d
: The day of the month (2 digits)%H
: The hour (2 digits, 24-hour clock)%M
: The minute (2 digits)%S
: The second (2 digits)%A
: The full weekday name%B
: The full month name%I
: The hour (2 digits, 12-hour clock)%p
: The AM/PM indicator
Here is an example of formatting a datetime
object as a string:
import datetime
# create a datetime object
dt = datetime.datetime(2022, 12, 24, 12, 34, 56)
# format the datetime object as a string
s = dt.strftime('%Y-%m-%d %H:%M:%S')
print(s) # Output: 2022-12-24 12:34:56
In this example, the strftime()
function formats the datetime
object as a string using the specified format.
You can also use the strftime()
function to format time
and date
objects as strings. For example:
import datetime
# create a date object
date = datetime.date(2022, 12, 24)
# format the date object as a string
s = date.strftime('%Y-%m-%d')
print(s) # Output: 2022-12-24
# create a time object
time = datetime.time(12, 34, 56)
# format the time object as a string
s = time.strftime('%H:%M:%S')
print(s) # Output: 12:34:56
In these examples, the strftime()
function formats the date
and time
objects as strings using the specified formats.
The datetime
module also provides the strptime()
function for parsing strings into datetime
objects. The strptime()
function uses format codes to specify the format of the input string. Here is an example of parsing a string into a datetime
object:
import datetime
# parse a string into a datetime object
dt = datetime.datetime.strptime('2022-12-24 12:34:56', '%Y-%m-%d %H:%M:%S')
print(dt) # Output: 2022-12-24 12:34:56
In this example, the strptime()
function parses the string into a datetime
object using the specified format.
You can also use the strptime()
function to parse strings into date
and time
objects. For example:
import datetime
# parse a string into a date object
date = datetime.datetime.strptime('2022-12-24', '%Y-%m-%d').date()
print(date) # Output: 2022-12-24
# parse a string into a time object
time = datetime.datetime.strptime('12:34:56', '%H:%M:%S').time()
print(time) # Output: 12:34:56
In these examples, the strptime()
function parses the strings into date
and time
objects using the specified formats.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
Use the datetime
module to format the datetime
object ‘2022-12-24 12:34:56’ as a string in the following format: ‘YYYY/MM/DD HH:MM:SS’.
import datetime
# create a datetime object
dt = datetime.datetime(2022, 12, 24, 12, 34, 56)
# format the datetime object as a string
s = dt.strftime('%Y/%m/%d %H:%M:%S')
print(s) # Output: 2022/12/24 12:34:56
Use the datetime
module to parse the string ‘2022/12/24 12:34:56’ into a datetime
object.
import datetime
# parse the string into a datetime object
dt = datetime.datetime.strptime('2022/12/24 12:34:56', '%Y/%m/%d %H:%M:%S')
print(dt) # Output: 2022-12-24 12:34:56
Use the datetime
module to create a datetime
object for the current date and time, and format it as a string in the following format: ‘MM/DD/YYYY’.
import datetime
# create a datetime object for the current date and time
dt = datetime.datetime.now()
# format the datetime object as a string
s = dt.strftime('%m/%d/%Y')
print(s) # Output: 01/20/2022 (or similar)
Use the datetime
module to parse the string ‘January 20, 2022’ into a datetime
object.
import datetime
# parse the string into a datetime object
dt = datetime.datetime.strptime('January 20, 2022', '%B %d, %Y')
print(dt) # Output: 2022-01-20 00:00:00
Use the datetime
module to create a timedelta
object that represents a duration of 1 day and 2 hours, and add it to a datetime
object for the current date and time.
import datetime
# create a datetime object for the current date and time
dt = datetime.datetime.now()
# create a timedelta object for 1 day and 2 hours
td = datetime.timedelta(days=1, hours=2)
# add the timedelta object to the datetime object
dt2 = dt + td
print(dt2) # Output: 2022-01-21 14:34:56.789012 (or similar)