どうも、ちょげ(@chogetarou)です。
現在時刻のみを文字列として取得する方法を紹介します。
方法

現在時刻のみを文字列として取得するには、datetimeを使います。
まず、datetimeをインポートします。
from datetime import datetime
次に、datetimeからnow()を呼び出します。
now()からstrftime()を呼び出します。
そして、stftime()の引数に「”%H:%M:%S”」を指定します。
current_time = datetime.now().strftime("%H:%M:%S")
上記のstrftime()は、現在時刻のみを文字列として返します。
使用例
from datetime import datetime
current_time = datetime.now().strftime("%H:%M:%S")
print(current_time) #21:41:21
コメント