How to get the current time in Python

Obtaining the current time is a common operation in programming. Python provides a variety of methods to obtain the current time. The following will introduce in detail several common methods of obtaining the current time in Python, and attach the corresponding source code.

Method 1: Use datetime module

The datetime module is Python's built-in module for processing dates and times. It provides the datetime class to represent dates and times. You can use the datetime class in the datetime module to get the current time.

import datetime

# 获取当前时间
current_time = datetime.datetime.now()

# 打印当前时间
print("当前时间:", current_time)

Running the above code will output the current date and time in the format of year-month-day hour:minute:second.

Method 2: Use the time module

The time module is also built into Python

Guess you like

Origin blog.csdn.net/ByteHackerX/article/details/133542202