我需要在python3中执行以下日期类型转换:
“2017 年 6 月 11 日 11:16:00 下午” -->“2017-11-06 23:16:00” 我尝试过几个库,但总是收到错误,即使解析器似乎正在工作
from dateutil import parser
dt = parser.parse("6/11/2017 11:16:00 p.m.")
print(dt)
输出:
2017-06-11 23:16:00
它交换日期和月份:(
提前致谢,
最佳答案
使用dayfirst
参数 -
:param
dayfirst
:
Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the day (True
) or month (False
). Ifyearfirst
is set toTrue
, this distinguishes between YDM and YMD. If set toNone
, this value is retrieved from the current :class:parserinfo
object (which itself defaults toFalse
).
dt = parser.parse("6/11/2017 11:16:00 p.m.", dayfirst=True)
dt
2017-11-06 23:16:00
请参阅dateutil.parser.parse
文档供引用。
关于python - python 解析器库中的月份和日期交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839339/