我在Python中看到了很多关于将日期字符串转换为datetime对象的内容,但我想采用另一种方式。 我有

datetime.datetime(2012, 2, 23, 0, 0)

我想把它转换成'2/23/2012'这样的字符串。


当前回答

到目前为止最性感的版本是格式字符串。

from datetime import datetime

print(f'{datetime.today():%Y-%m-%d}')

其他回答

类型特定的格式也可以使用:

t = datetime.datetime(2012, 2, 23, 0, 0)
"{:%m/%d/%Y}".format(t)

输出:

'02/23/2012'
end_date = "2021-04-18 16:00:00"
end_date_string = end_date.strftime("%Y-%m-%d")
print(end_date_string)

通过直接使用datetime对象的组件,可以将datetime对象转换为字符串。

from datetime import date  

myDate = date.today()    
#print(myDate) would output 2017-05-23 because that is today
#reassign the myDate variable to myDate = myDate.month 
#then you could print(myDate.month) and you would get 5 as an integer
dateStr = str(myDate.month)+ "/" + str(myDate.day) + "/" + str(myDate.year)    
# myDate.month is equal to 5 as an integer, i use str() to change it to a 
# string I add(+)the "/" so now I have "5/" then myDate.day is 23 as
# an integer i change it to a string with str() and it is added to the "5/"   
# to get "5/23" and then I add another "/" now we have "5/23/" next is the 
# year which is 2017 as an integer, I use the function str() to change it to 
# a string and add it to the rest of the string.  Now we have "5/23/2017" as 
# a string. The final line prints the string.

print(dateStr)  

产出——> 5/23/2017

Date和datetime对象(以及time)支持一种迷你语言来指定输出,并且有两种方式来访问它:

直接方法调用:dt。strftime(这里的格式) 格式化方法(python 2.6+): '{: Format here}'.format(dt) f-strings (python 3.6+): f'{dt:format here}'

所以你的例子可以是这样的:

dt。strftime('日期是%b %d, %Y') '日期为{:%b %d, %Y}'.format(dt) f'日期为{dt:%b %d, %Y}'

在这三种情况下,输出结果是:

日期是2012年2月23日

为了完整起见:你也可以直接访问对象的属性,但这样你只能得到数字:

'The date is %s/%s/%s' % (dt.month, dt.day, dt.year)
# The date is 02/23/2012

学习这种迷你语言所花的时间是值得的。


作为参考,下面是迷你语言中使用的代码:

%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.

我已经使用此方法将日期插入JSON对象

my_json_string = json.dumps({'date_of_birth': '''{}'''.format(date_of_birth)})