-1

I am using Scrapy to parse data and getting date in Jun 14, 2016 format, I have tried to parse it with datetime.strftime but

what approach should I use to convert custom date strings and what to do in my case.

UPDATE

I want to parse UNIX timestamp to save in database.

Aren Hovsepyan
  • 1,947
  • 2
  • 17
  • 45
  • What format do you want the string in? – Harrison Sep 02 '16 at 14:22
  • I usually use `datetime.datetime.strptime(your_date, "%b %d, %Y")` – Andrew Sep 02 '16 at 14:24
  • @Andrew: that answers the question. – Jean-François Fabre Sep 02 '16 at 14:26
  • I tried, with @Andrew's answer, `datetime.strptime(date,"%b %d %Y")`, but no result, and my date is for example `Jun 14, 2016` – Aren Hovsepyan Sep 02 '16 at 14:27
  • After this step, you'll have to use `datetime_object.strftime("Your desired output format goes here")` to save in the format you desire. You haven't given any concrete indication of what you want your output to look like, but the `strptime` function will create a datetime object for you to format as you wish. – Andrew Sep 02 '16 at 14:29
  • @Andrew, I updated post, that I want UNIX timestamp – Aren Hovsepyan Sep 02 '16 at 14:31
  • what have you tried already? I don't know what a UNIX timestamp looks like. – Andrew Sep 02 '16 at 14:33
  • UNIX timestamp is normally seconds or milliseconds since epoch, Try this answer here : http://stackoverflow.com/questions/6999726/how-can-i-convert-a-datetime-object-to-milliseconds-since-epoch-unix-time-in-p You just need to pass your datetime object into the function defined in the answer – R.Sharp Sep 02 '16 at 14:37

1 Answers1

0

Something like this should work:

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

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

Returns: 1472792400

ode2k
  • 2,653
  • 13
  • 20