如果你希望在年、月、日、時、分和秒之間使用短橫線(-)進(jìn)行分隔,你可以使用字符串的format
方法或者f-string來構(gòu)建輸出字符串。以下是修改后的代碼:
使用字符串的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("當(dāng)前時間:{}".format(formatted_date))
使用f-string:文章來源:http://www.zghlxwxcb.cn/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"當(dāng)前時間:{formatted_date}")
無論你選擇哪種方法,它們都會輸出類似于"當(dāng)前時間:2023-05-16 14:30:59"的結(jié)果,其中年、月、日、時、分和秒之間使用短橫線進(jìn)行分隔。文章來源地址http://www.zghlxwxcb.cn/news/detail-623870.html
到了這里,關(guān)于python獲取當(dāng)前年月日時分秒,格式(YYYY-HH-HH HH-FF-SS)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!