属性错误:"日期时间.日期"对象的属性"月"不可写



我只是想将月份格式转换为字符串。默认格式为2022-07-20,我希望7输出为"July"这是我自己的代码,请纠正并建议我放入正确的代码。提前谢谢你。

def get_currentDate():
today = date.today()
if today.month == 7:
today.month = "July"
print("It's {} {}, {} sir".format(today.month, today.day, today.year))

你不能改变今天的日、月、年的值,所以你可以把它做成一个列表(你可以改变它的值)

def get_currentDate():
today = date.today()
today = [today.year, today.month, today.day]
if today[1] == 7:
today[1] = "July"
print("It's {} {}, {} sir".format(today[1], today[2], today[0]))

我已经提出了这段代码,似乎我得到了我想要的输出,我也在我的输出中添加了一些文本。

def get_currentDate():
this_day = date.today()
day = int(this_day.strftime("%d"))
if day == 1 or day == 21 or day == 31:
print(this_day.strftime("It's %A, {}st of %b %Y ").format(day))
elif day == 2 or day == 22:
print(this_day.strftime("It's %A, {}nd of %b %Y ").format(day))
elif day == 3 or day == 23:
print(this_day.strftime("It's %A, {}rd of %b %Y ").format(day))
elif day > 3:
print(this_day.strftime("It's %A, {}th of %b %Y ").format(day))
如果我说错了,或者有什么建议,请纠正我。谢谢你

相关内容

  • 没有找到相关文章

最新更新