【问题标题】:How to get date from this format "Tue May 15 2018 00:00:00 GMT-0400 (EDT)" in python如何在python中从这种格式“Tue May 15 2018 00:00:00 GMT-0400 (EDT)”获取日期
【发布时间】:2018-10-12 23:58:25
【问题描述】:

如何将日期从“Tue May 15 2018 00:00:00 GMT-0400 (EDT)”转换为 yyyy-mm-dd 格式的日期?

【问题讨论】:

标签: python django python-3.x python-2.7


【解决方案1】:

dateutil是你的朋友:

>>> import dateutil.parser

>>> dt=dateutil.parser.parse('Tue May 15 2018 00:00:00 GMT-0400 (EDT)')
datetime.datetime(2018, 5, 15, 0, 0, tzinfo=tzoffset('EDT', 14400))

>>> dt.strftime('%Y-%m-%d')
'2018-05-15'

【讨论】:

    【解决方案2】:

    你可以试试:

    from datetime import datetime
    
        date_converted = datetime.strptime(time_string, '%Y-%m-%d')
    


    更多信息here、docs 和 here。

    【讨论】:

    • 该问题专门要求提供给定的格式。这不适用于提供的字符串。如果您调整问题中给出的格式会有所帮助。
    猜你喜欢
    • 2019-12-02
    • 1970-01-01
    • 2023-03-07
    • 2021-07-29
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    相关资源
    最近更新 更多