You can download this code by clicking the button below.
This code is now available for download.
Retrieve the current time and output it in a specified format.
Technology Stack : datetime
Code Type : Function
Code Difficulty : Intermediate
import datetime
def get_current_time_with_format(date_format="%Y-%m-%d %H:%M:%S"):
"""
获取当前时间并格式化输出。
Args:
date_format (str): 时间格式,默认为 "%Y-%m-%d %H:%M:%S"。
Returns:
str: 格式化后的当前时间字符串。
"""
current_time = datetime.datetime.now()
formatted_time = current_time.strftime(date_format)
return formatted_time