gpt4 book ai didi

python - 日期格式化为月份大写

转载 作者:行者123 更新时间:2023-12-05 08:14:23 25 4
gpt4 key购买 nike

我设法在

import datetime
getDate = datetime.date.today()
print(getDate.strftime("%Y-%B-%d"))

输出为 2018-June-23

但我想像这样格式化输出:2018-JUNE-23(月份是大写的)

最佳答案

要直接在格式字符串中执行此操作,请在前面加上 carrot当月 (^):

>>> getDate = datetime.date.today()
>>> print(getDate.strftime("%Y-%^B-%d"))
2018-JUNE-22

注意:如果您的平台 strftime 上有可用的 glibc 扩展(或等效功能),则此方法有效。您可以调用 man strftime 进行检查.如果它不能在你的平台上运行,或者你需要保证跨平台的行为,那么更喜欢在这里使用 str.upper 作为 shown in the other answer 进行额外的函数调用。 .

关于python - 日期格式化为月份大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50994144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com