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

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

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


当前回答

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

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

输出:

'02/23/2012'

其他回答

通过直接使用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

如果你也想要时间,那就去吧

datetime.datetime.now().__str__()

打印2019-07-11 19:36:31.118766在控制台为我

你可以使用简单的字符串格式化方法:

>>> dt = datetime.datetime(2012, 2, 23, 0, 0)
>>> '{0.month}/{0.day}/{0.year}'.format(dt)
'2/23/2012'
>>> '%s/%s/%s' % (dt.month, dt.day, dt.year)
'2/23/2012'

你可以用下面的方法将datetime转换为字符串:

from datetime import datetime

date_time = datetime(2012, 2, 23, 0, 0)
date = date_time.strftime('%m/%d/%Y')
print("date: %s" % date)

以下是一些可以用来将datetime转换为字符串的模式:

为了更好地理解,您可以参考这篇关于如何在Python或官方strftime文档中将字符串转换为datetime和datetime转换为字符串的文章

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.