「Python3.9」現在時間を取得するサンプル

環境
Python 3.9.13
Windows 11 Pro 21H2 64bit
PyCharm 2022.2.1 (Community Edition)

書式
datetime.now().strftime(出力形式)
出力形式を指定する主な文字列
%Y 年 4桁
%y 年 2桁
%m 月 2桁
%d 日 2桁
%H 時 24時間表記
%M 分 2桁
%S 秒 2桁
%f マイクロ秒(6桁)
%X 時:分:秒

使用例

# coding: utf-8
import datetime 

# 現在時刻の取得
e = datetime.datetime.now().strftime('%X')
print("現在時刻")
print(e)

f = datetime.datetime.now().strftime('%Y')
print(f)

m= datetime.datetime.now().strftime('%m')
print(m)

実行結果
現在時刻
22:50:15
2022
09

Python

Posted by arkgame