どうも、ちょげ(@chogetarou)です。
DateTimeから今年の年を取得する方法を紹介します。
方法

DateTimeから今年の年を取得するには、now()とyearを使います。
まず、datetimeからnow()を呼び出します。
そして、now()の結果のyearプロパティにアクセスします。
current_year = datetime.now().year
datetime.now().yearは、今年の年を返します。
使用例
from datetime import datetime
current_year = datetime.now().year
print(current_year) #2022
print(type(current_year)) #<class 'int'>
コメント