我正在嘗試將當前月份編號轉換為月份名稱。我讀過類似的帖子,但是當我嘗試傳統方法時出現錯誤,我不知道是什么導致了錯誤。這是我的代碼:
month_name = datetime.now().month
month_name.strftime("%B")
我收到了這個錯誤:
AttributeError Traceback (most recent call last)
Input In [202], in <cell line: 2>()
1 month_name = datetime.now().month
----> 2 month_name.strftime("%B")
AttributeError: 'int' object has no attribute 'strftime'
我該如何解決這個問題?謝謝你。
uj5u.com熱心網友回復:
您需要存盤與您獲得的月份相對應的日期datetime.now():
from datetime import datetime
date = datetime.now()
month_number = date.month
print(date.strftime("%B"), month_number)
輸出:
June 6
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496835.html
