【问题标题】:Returning strings instead of digits with datetime.strptime使用 datetime.strptime 返回字符串而不是数字
【发布时间】:2018-02-28 12:46:24
【问题描述】:

使用以下代码:

current_date = datetime.strptime(row[0], "%Y-%m-%d")
dates.append(current_date)

我想以字符串格式(即"Jan 2014")返回日期,但它以"2014-01-01" 的原始格式返回日期。

【问题讨论】:

标签: python python-3.x datetime strptime


【解决方案1】:

date 对象的默认字符串表示是一个表示 ISO 8601 格式的日期的字符串,'YYYY-MM-DD'。要以其他格式表示日期,请使用date.strftime(),例如:

>>> from datetime import date
>>> date.today().strftime('%b %d, %Y')
'Sep 19, 2017'

有关格式化指令的完整列表,请参阅strftime() and strptime() Behavior

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2019-09-15
    相关资源
    最近更新 更多