You can format a datetime object using strftime
method of the datetime library. Here is an example code snippet that formats a datetime in the mentioned format:
main.py147 chars7 lines
In this code, we create a datetime object now
using datetime.now()
and then use its strftime
method to format it as per our requirement. The strftime
formatting codes used in the above example are:
%B
: Full month name%d
: Day of the month (zero padded)%Y
: Year (4 digits)%I
: Hour (12-hour clock) (zero padded)%M
: Minute (zero padded)%S
: Second (zero padded)%f
: Microsecond (zero padded)%p
: AM/PM (upper case)This will output the current date and time in the format "Month Day, Year Hour:Minute:Second.Microsecond AM/PM".
gistlibby LogSnag