Pythonで時間をプリントする方法は何ですか?

Pythonでは、時間を出力するためにdatetimeモジュールを使用することができます。以下はサンプルコードです:

import datetime

current_time = datetime.datetime.now()
print("当前时间:", current_time)

結果は次のようになります:

当前时间: 2022-06-01 10:30:15.123456

strftimeメソッドを使用して、時間をフォーマットして印刷することもできます。以下に例を示します:

import datetime

current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
print("当前时间:", formatted_time)

結果は次のようになります:

当前时间: 2022-06-01 10:30:15

strftimeメソッドでは、%Yは4桁の年、%mは2桁の月、%dは2桁の日、%Hは24時間表記の時、%Mは分、%Sは秒を表します。これらのフォーマット文字を自由に組み合わせて、必要に応じて使用することができます。

bannerAds