Convert datetime object to a String of date only in Python
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities
--
Chapters
00:00 Convert Datetime Object To A String Of Date Only In Python
00:23 Accepted Answer Score 778
00:37 Answer 2 Score 312
02:54 Answer 3 Score 75
03:04 Answer 4 Score 26
03:33 Thank you
--
Full question
https://stackoverflow.com/questions/1062...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #datetime
#avk47
ACCEPTED ANSWER
Score 778
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
ANSWER 2
Score 312
date and datetime objects (and time as well) support a mini-language to specify output, and there are two ways to access it:
- direct method call: 
dt.strftime('format here') - format method (python 2.6+): 
'{:format here}'.format(dt) - f-strings (python 3.6+): 
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:
%aWeekday as locale’s abbreviated name.%AWeekday as locale’s full name.%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.%dDay of the month as a zero-padded decimal number.%bMonth as locale’s abbreviated name.%BMonth as locale’s full name.%mMonth as a zero-padded decimal number. 01, ..., 12%yYear without century as a zero-padded decimal number. 00, ..., 99%YYear with century as a decimal number. 1970, 1988, 2001, 2013%HHour (24-hour clock) as a zero-padded decimal number. 00, ..., 23%IHour (12-hour clock) as a zero-padded decimal number. 01, ..., 12%pLocale’s equivalent of either AM or PM.%MMinute as a zero-padded decimal number. 00, ..., 59%SSecond as a zero-padded decimal number. 00, ..., 59%fMicrosecond as a decimal number, zero-padded on the left. 000000, ..., 999999%zUTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030%ZTime zone name (empty if naive), UTC, EST, CST%jDay of the year as a zero-padded decimal number. 001, ..., 366%UWeek number of the year (Sunday is the first) as a zero padded decimal number.%WWeek number of the year (Monday is first) as a decimal number.%cLocale’s appropriate date and time representation.%xLocale’s appropriate date representation.%XLocale’s appropriate time representation.%%A literal '%' character.
ANSWER 3
Score 75
Another option:
import datetime
now=datetime.datetime.now()
now.isoformat()
# ouptut --> '2016-03-09T08:18:20.860968'
ANSWER 4
Score 26
If you are looking for a simple way of datetime to string conversion and can omit the format. You can convert datetime object to str and then use array slicing.
In [1]: from datetime import datetime
In [2]: now = datetime.now()
In [3]: str(now)
Out[3]: '2019-04-26 18:03:50.941332'
In [5]: str(now)[:10]
Out[5]: '2019-04-26'
In [6]: str(now)[:19]
Out[6]: '2019-04-26 18:03:50'
But note the following thing. If other solutions will rise an AttributeError when the variable is None in this case you will receive a 'None' string.
In [9]: str(None)[:19]
Out[9]: 'None'