如果你希望在年、月、日、时、分和秒之间使用短横线(-)进行分隔,你可以使用字符串的format
方法或者f-string来构建输出字符串。以下是修改后的代码:
使用字符串的format
方法:
from datetime import datetime
now = datetime.now()
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
formatted_date = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(year, month, day, hour, minute, second)
print("当前时间:{}".format(formatted_date))
使用f-string:
from datetime import datetime
now = datetime.now()
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
formatted_date = f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}"
print(f"当前时间:{formatted_date}")
无论你选择哪种方法,它们都会输出类似于"当前时间:2023-05-16 14:30:59"的结果,其中年、月、日、时、分和秒之间使用短横线进行分隔。