What is the method to print time in Python?

In Python, you can use the datetime module to print the current time. Here is an example code:

import datetime

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

The output is similar to:

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

You can also use the strftime method to format and print time. Here is an example code:

import datetime

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

The output result is similar to:

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

In the strftime method, %Y represents a four-digit year, %m represents a two-digit month, %d represents a two-digit date, %H represents the hour in 24-hour format, %M represents minutes, and %S represents seconds. You can freely combine these formatting characters to meet your needs.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds