[Python]datetimeで今年の年を取得するには?

python

どうも、ちょげ(@chogetarou)です。

DateTimeから今年の年を取得する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

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'>

コメント

タイトルとURLをコピーしました