【问题标题】:converting string into date format with Python使用 Python 将字符串转换为日期格式
【发布时间】:2021-02-03 01:11:16
【问题描述】:

您好,我正在尝试在 Python 中将此字符串 : '2019-01-10' 转换为日期格式为 2019-01-10

我试过这段代码:

from datetime import datetime

date_time_str = '2019-01-10'

date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d )

但我有一个 ValueError:时间数据 '2019-01-10' 与格式 '%y-%m-%d' 不匹配

你能帮帮我吗?

【问题讨论】:

    标签: python string date datetime type-conversion


    【解决方案1】:
    from datetime import datetime
    date_time_str = '2018-06-29'
    date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%d')
    

    尝试将其更改为上述代码。

    【讨论】:

      【解决方案2】:

      %y 格式说明符为 2 位,使用大写 %Y。参考:https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-28
        • 1970-01-01
        • 2014-01-18
        • 2019-10-13
        相关资源
        最近更新 更多