如果你希望在年、月、日、时、分和秒之间使用短横线(-)进行分隔,你可以使用字符串的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:文章来源:https://www.toymoban.com/news/detail-623870.html
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"的结果,其中年、月、日、时、分和秒之间使用短横线进行分隔。文章来源地址https://www.toymoban.com/news/detail-623870.html
到了这里,关于python获取当前年月日时分秒,格式(YYYY-HH-HH HH-FF-SS)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!