I see a lot on converting a date string to an datetime
object in Python, but I want to go the other way.
I've got
datetime.datetime(2012, 2, 23, 0, 0)
and I would like to convert it to string like '2/23/2012'
.
To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.
For this, we will use the strptime() method. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function.
To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.
You can use strftime to help you format your date.
E.g.,
import datetime t = datetime.datetime(2012, 2, 23, 0, 0) t.strftime('%m/%d/%Y')
will yield:
'02/23/2012'
More information about formatting see here
date
and datetime
objects (and time
as well) support a mini-language to specify output, and there are two ways to access it:
dt.strftime('format here')
'{:format here}'.format(dt)
f'{dt:format here}'
So your example could look like:
dt.strftime('The date is %b %d, %Y')
'The date is {:%b %d, %Y}'.format(dt)
f'The date is {dt:%b %d, %Y}'
In all three cases the output is:
The date is Feb 23, 2012
For completeness' sake: you can also directly access the attributes of the object, but then you only get the numbers:
'The date is %s/%s/%s' % (dt.month, dt.day, dt.year) # The date is 02/23/2012
The time taken to learn the mini-language is worth it.
For reference, here are the codes used in the mini-language:
%a
Weekday as locale’s abbreviated name.%A
Weekday as locale’s full name.%w
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.%d
Day of the month as a zero-padded decimal number.%b
Month as locale’s abbreviated name.%B
Month as locale’s full name.%m
Month as a zero-padded decimal number. 01, ..., 12%y
Year without century as a zero-padded decimal number. 00, ..., 99%Y
Year with century as a decimal number. 1970, 1988, 2001, 2013%H
Hour (24-hour clock) as a zero-padded decimal number. 00, ..., 23%I
Hour (12-hour clock) as a zero-padded decimal number. 01, ..., 12%p
Locale’s equivalent of either AM or PM.%M
Minute as a zero-padded decimal number. 00, ..., 59%S
Second as a zero-padded decimal number. 00, ..., 59%f
Microsecond as a decimal number, zero-padded on the left. 000000, ..., 999999%z
UTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030%Z
Time zone name (empty if naive), UTC, EST, CST%j
Day of the year as a zero-padded decimal number. 001, ..., 366%U
Week number of the year (Sunday is the first) as a zero padded decimal number.%W
Week number of the year (Monday is first) as a decimal number.%c
Locale’s appropriate date and time representation.%x
Locale’s appropriate date representation.%X
Locale’s appropriate time representation.%%
A literal '%' character.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With