Going from twitter date to Python datetime date Going from twitter date to Python datetime date python python

Going from twitter date to Python datetime date


Writing something like this should convert a twitter date to a timestamp.

import timets = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))

UPDATE

For Python 3, as per 2020, you can do it in this way:

from datetime import datetime# dtime = tweet['created_at']dtime = 'Fri Oct 09 10:01:41 +0000 2015'new_datetime = datetime.strftime(datetime.strptime(dtime,'%a %b %d %H:%M:%S +0000 %Y'), '%Y-%m-%d %H:%M:%S')print((new_datetime))


Give this a go. It assumes the date format from twitter is RFC822 compliant (see the question linked to by @Adrien).

A naive datetime object is constructed (i.e. no timezone info). It is adjusted according to the timezone offset to UTC. Unless you have a need to keep the original timezone, I'd store the date time as UTC and format to local time when you display it.

from datetime import datetime, timedeltafrom email.utils import parsedate_tzs = 'Tue Mar 29 08:11:25 +0000 2011'def to_datetime(datestring):    time_tuple = parsedate_tz(datestring.strip())    dt = datetime(*time_tuple[:6])    return dt - timedelta(seconds=time_tuple[-1])


A little bit old but using parse really help me with this issue

from datetime import datetimefrom dateutil.parser import parsedate = 'Fri May 10 00:44:04 +0000 2019' dt = parse(date)print(dt) # 2019-05-10 00:44:04+00:00