首页 > 解决方案 > strftime 返回 ValueError

问题描述

strftime 返回值错误。为什么?
这是代码

datetime.datetime.now().strftime('%-m')

我的输出:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format string

标签: pythondatetime

解决方案


基于strftime.org格式可以依赖系统。

在 Unix 上可能需要-like"%-m"但在 Windows 上可能需要#like "%#m".

我只能在 Linux 上对其进行测试,并且两者都对我有用:"%-m %#m"


顺便说一句:但如果你想获得负值,那么你需要-%喜欢"-%m"


推荐阅读