python get current time to milliseconds

To get the current time to milliseconds, you can use the strftime method to format the current time into a string with milliseconds on the basis of getting the current time. The specific code is as follows:

import datetime

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

# 格式化输出带有毫秒的当前时间字符串
current_time_str = current_time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
print(current_time_str)

The above code obtains milliseconds by adding "%f" in the format string, indicating that %f represents milliseconds, and when formatting output, you need to use [3:-3] to intercept the microseconds in the string. Running the above code will output the current time and format it into a string format of "year-month-day hour:minute:second.millisecond".

Guess you like

Origin blog.csdn.net/qq_42629529/article/details/131008232