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

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

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


当前回答

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.

其他回答

另一个选择:

import datetime
now=datetime.datetime.now()
now.isoformat()
# ouptut --> '2016-03-09T08:18:20.860968'

字符串连接str.join可用于构建字符串。

d = datetime.now()
'/'.join(str(x) for x in (d.month, d.day, d.year))
'3/7/2016'

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.

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

datetime.datetime.now().__str__()

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

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