python 現在日時を取得する
pythonで、datetimeを使用して、現在日時を取得するサンプルコードを記述してます。
環境
- OS windows10 pro 64bit
- python 3.8.5
現在日時を取得
datetimeをimportして、現在日時を取得します。
import datetime
dt = datetime.datetime.now()
# 現在日付
print(dt)
# 2020-12-05 17:25:52.019257
# 年
print(dt.year)
# 2020
# 月
print(dt.month)
# 12
# 日
print(dt.day)
# 5
# 時
print(dt.hour)
# 17
# 分
print(dt.minute)
# 25
# 秒
print(dt.second)
# 52
# マイクロ秒
print(dt.microsecond)
# 19257
フォーマットを指定して、出力する場合は以下となります。
print(dt.strftime('%Y年%m月%d日 %H:%M:%S'))
# 2020年12月05日 17:30:52
現在日時は以下で取得可能です。
import datetime
dt = datetime.date.today()
print(dt)
# 2020-12-05
print(dt.strftime('%Y年%m月%d日'))
# 2020年12月05日
-
前の記事
Windows10 grepをインストールして使用する手順 2020.12.16
-
次の記事
php powで数値のn乗を計算する 2020.12.16
コメントを書く