【问题标题】:How to convet string to datetime in python? [duplicate]如何在python中将字符串转换为日期时间? [复制]
【发布时间】:2019-12-15 11:06:59
【问题描述】:
now = datetime.strptime(str(col[4].text), '%d.%m.%Y, %H:%M:%S')

print(type(now))

但是输出说现在变量的类型是:class 'datetime.datetime'

各位,我不知道如何证明我的专有技术,有人可以帮忙吗?

【问题讨论】:

  • datetime.strptime 的文档清楚地表明将返回 datetime。你期待什么?
  • 你要什么?您正在尝试做的事情有问题吗?

标签: python


【解决方案1】:

日期时间对象在打印时具有默认格式。 strptime 的格式部分定义了日期时间开始的字符串格式。一旦它被转换为日期时间,它就会使用__repr____str__ 定义的标准表示。

要将这些值转换为所需格式的字符串,可以使用strftime

now = datetime.strptime(str(col[4].text), '%d.%m.%Y, %H:%M:%S')
now = now.strftime('%d.%m.%Y, %H:%M:%S')

日期时间对象的文档指出print(datetime_object) 将返回datetime_object 的ISO 格式表示。 ISO 格式是您在打印日期时间时看到的 YYYY-MM-DD 格式。

在此处阅读更多信息:https://docs.python.org/3.2/library/datetime.html

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2018-03-24
    • 2011-07-10
    • 2016-03-08
    • 2012-09-22
    • 2014-02-04
    • 2020-01-02
    • 2023-02-17
    相关资源
    最近更新 更多