【问题标题】:Custom date string to Python date object自定义日期字符串到 Python 日期对象
【发布时间】:2017-01-10 16:56:18
【问题描述】:

我正在使用 Scrapy 解析数据并获取 Jun 14, 2016 格式的日期,我尝试使用 datetime.strftime 解析它,但是

我应该使用什么方法来转换自定义日期字符串以及在我的情况下该怎么做。

更新

我想解析 UNIX 时间戳以保存在数据库中。

【问题讨论】:

  • 你想要什么格式的字符串?
  • 我一般用datetime.datetime.strptime(your_date, "%b %d, %Y")
  • @Andrew:回答了这个问题。
  • 我试过了,@Andrew 的回答是datetime.strptime(date,"%b %d %Y"),但没有结果,例如我的日期是Jun 14, 2016
  • 在这一步之后,您必须使用datetime_object.strftime("Your desired output format goes here") 以您想要的格式保存。您没有具体说明您希望输出是什么样的,但strptime 函数将创建一个日期时间对象,供您根据需要进行格式化。

标签: python date converter python-3.5


【解决方案1】:

这样的事情应该可以工作:

import time
import datetime
datestring = "September 2, 2016"

unixdatetime = int(time.mktime(datetime.datetime.strptime(datestring, "%B %d, %Y").timetuple()))
print(unixdatetime)

返回:1472792400

【讨论】:

    猜你喜欢
    • 2011-02-17
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    相关资源
    最近更新 更多