strptime和strftime中参数的不同顺序

时间:2012-02-20 13:17:25

标签: python strftime strptime

time.strftime(format[, t])中,第一个参数是format,而在time.strptime(string[, format])中,它是第二个参数。为什么会这样?有时我感到困惑并无意中使用format作为time.strptime中的第一个参数,这引发了错误。

1 个答案:

答案 0 :(得分:1)

一般原则是你在可选参数之前放置必需的参数(事实上,只有当你使用关键字参数时才能在可选参数之前放置可选参数,time.strftimetime.strptime不支持。)由于time.strftime(format)格式化当前时间,因此使用的可选时间而不是当前时间必须是第二个参数。同样,由于time.strptime(string)根据默认格式解析stringformat必须是第二个参数。

相关问题