Python: 現在の年・月・日・時間・分・秒を個別に取得
年・月・日・時間・分・秒を個別に取得
Pythonで現在の日時を個別に取得する場合は、 まず現在日時を取得して、 それから 属性のyear, month, day などを使って個別に取得できます。
属性 | 概要 |
---|---|
year | 年 |
month | 月 |
day | 日 |
hour | 時間 |
minute | 分 |
second | 秒 |
microsecond | マイクロ秒 |
import datetime # 現在の日時 dt = datetime.datetime.now() print(dt) # 2020-11-12 04:20:23.381492 # 年 print(dt.year) # 2020 # 月 print(dt.month) # 11 # 日 print(dt.day) # 12 # 時間 print(dt.hour) # 4 # 分 print(dt.minute) # 20 # 秒 print(dt.second) # 23 # マイクロ秒 print(dt.microsecond) # 381492