Python method to get the current time accurate to seconds

To get the current time in Python and limit the precision to seconds, you can use the datetime module. Below are detailed steps and corresponding source code examples:

First, you need to import the datetime module:

import datetime

Then, you can use the datetime class in the datetime module to get the current time. Calling the now() method of the datetime class returns a datetime object representing the current time. Next, the datetime object can be formatted into the desired time string using the strftime() method.

The following is a source code example to get the current time accurate to seconds:

import datetime

current_time = datetime.datetime.now()
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")

Guess you like

Origin blog.csdn.net/2301_78484069/article/details/133556665