ctime() returns the date and time in the classic C ctime() layout: weekday, month, day, time, and year, as in 'Fri Mar 15 10:30:45 2024'. The result is always 24 characters long.
Single-digit days are padded with a space, not a zero ('Sun Mar 3'). There are no options; for any other layout use strftime().
datetime.ctime() is an instance method of datetime.datetime: call it on an existing object with dot notation, such as moment.ctime(...). Import the class first, for example from datetime import datetime. The date class has a method with the same name.
Syntax
Call it with no arguments.
python
text = moment.ctime()
text = day.ctime()
Call
What it does
datetime(2024, 3, 15, 10, 30, 45).ctime()
Returns 'Fri Mar 15 10:30:45 2024'.
datetime(2024, 3, 3, 8, 5).ctime()
Returns 'Sun Mar 3 08:05:00 2024': the day is space-padded.
date(2024, 3, 15).ctime()
Returns 'Fri Mar 15 00:00:00 2024': a date shows midnight.
It matches time.ctime() output, so it is handy when imitating Unix log lines.
Method signatures
python
datetime.ctime()
date.ctime()
# returns: str like 'Fri Mar 15 10:30:45 2024'
Parameters
ctime() takes no parameters.
Return Value
Return Value
Description
str
A 24-character string in the form 'Www Mmm dd hh:mm:ss yyyy'.
Python Examples
Example 1 — See the ctime() Layout
Format a two-digit day, a one-digit day, and a plain date.