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.

ExampleEdit & Run

datetime objects

from datetime import datetime

dt = datetime.now()

print(dt)
Output:
2024-12-03 10:12:02.808482[Finished in 0.012660670094192028s]

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

ExampleEdit & Run

instantiating datetime object

from datetime import datetime

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

print(dt)
Output:
2023-07-08 23:30:20[Finished in 0.01188211003318429s]

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.    

Syntax:
ctime()

The method takes no argument.

ExampleEdit & Run

using the ctime() method

from datetime import datetime

dt = datetime.now()

print(dt.ctime())
Output:
Tue Dec  3 10:12:02 2024[Finished in 0.012257612077519298s]
ExampleEdit & Run
from datetime import datetime

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

print(dt.ctime())
Output:
Tue Mar  7 10:15:30 2023[Finished in 0.011811078060418367s]

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. 

Syntax:
strftime(format)

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

ExampleEdit & Run
from datetime import datetime

now = datetime.now()

print(now.strftime("%B %d %Y %H:%M:%S"))
Output:
December 03 2024 10:12:02[Finished in 0.01135364593937993s]
ExampleEdit & Run
from datetime import datetime

now = datetime.now()

print(now.strftime("%Y/%m/%d %H:%M:%S"))
Output:
2024/12/03 10:12:02[Finished in 0.011452866019681096s]

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.
ExampleEdit & Run
import datetime 

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

print(dt.strftime("%A, %B %d %Y %I:%M %p"))
Output:
Tuesday, April 18 2023 03:30 PM[Finished in 0.012241112068295479s]