In this article, you will learn how to convert times from various representations into strings.

datetime objects

The datetime module in Python's standard library provide various classes for representing date and time values.  The datetime class  in the module is one of the popular classes for representing and manipulating dates and time.

datetime objects

from datetime import datetime

dt = datetime.now()

print(dt)

The now() class method is used to get the current date and time.

instantiating datetime object

from datetime import datetime

dt = datetime(year = 2023, month = 7, day = 8, hour = 23, minute = 30, second = 20)

print(dt)

As shown in the above examples, the default display format for datetime objects is YYYY-MM-DD HH:MM:SS. This is often referred to as the ISO 8601 format. In the next parts we will see how we can represent the dates and time in different and more flexible formats.

The ctime() method

The ctime() method is used to convert a datetime object into a string in the C-standard format i.e “Mon DD HH:MM:SS YYYY”. Where “Mon” stands for the abbreviated month name, “DD” for the day of the month, “HH:MM:SS” for the time, and “YYYY” for the year.    

ctime()

The method takes no argument.

using the ctime() method

from datetime import datetime

dt = datetime.now()

print(dt.ctime())
from datetime import datetime

dt = datetime(year = 2023, month = 3, day = 7, hour = 10, minute = 15, second = 30)

print(dt.ctime())

The strftime() method

The strftime() method  is a lot more flexible as it  allows us to define a custom format for the represented time unlike the ctime() function whose format is already predefined and cannot be altered. 

strftime(format)

The format argument is a string containing placeholders which will be replaced with corresponding  datetime values.

from datetime import datetime

now = datetime.now()

print(now.strftime("%B %d %Y %H:%M:%S"))
from datetime import datetime

now = datetime.now()

print(now.strftime("%Y/%m/%d %H:%M:%S"))

The following table summarizes the various formatting parameters.

parameter description
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%d The day of the month [0, 31]
%H Hour (24-hour clock) as a decimal number [00,23]
%I Hour (12-hour clock) as a decimal number [01,12]
%j Day of the year as a decimal number [001,366]
%m Month as a decimal number [01,12]
%M Minute as a decimal number [00,59]
%p Locale’s equivalent of either AM or PM
%S Second as a decimal number [00,61]
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]
%w Weekday as a decimal number [0(Sunday),6]
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]
%x Date representation.
%X Time representation.
%y Year without century, [00 - 99]
%Y Year with century as a decimal number
%z UTC offset in the form +HHMM or -HHMM
%Z Timezone name.
import datetime 

#formatting the date
dt = datetime.datetime(2023, 4, 18, 15, 30, 45)

print(dt.strftime("%A, %B %d %Y %I:%M %p"))