Python无效格式字符串

2024-04-19 15:54:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用strftime以以下格式打印日期: 2007年3月6日下午5:40

我写了以下代码:

import time
print time.strftime("%m-%d-%Y %T:%M%p")

但它给出了一个错误“无效格式字符串”。为什么?


Tags: 字符串代码importtime格式错误printstrftime
2条回答

也许你在找hourH而不是T。检查strftime可能的选项

print time.strftime("%m-%d-%Y %H:%M%p")

注意: 在Windows和Linux机器上的行为是不同的。如果您看到Windows平台支持的所有内容,请检查http://msdn.microsoft.com/en-us/library/fe06s4ak.aspx

所以在Windows上使用%H:%M:%S比使用%T要好。

中的%T:

print time.strftime("%m-%d-%Y %T:%M%p")

不存在。您应该使用%H(0-23)或%I(0-11)

相关问题 更多 >