Sanky Cse
Sanky Cse

Reputation: 171

Converting Twitter's date format to Datetime in python

date time format returned by Twitter is in this form:

Thu Apr 23 13:38:19 +0000 2009

I want it in datetime format for database enty and query...

Upvotes: 8

Views: 10453

Answers (3)

Chris
Chris

Reputation: 1

Keeping it simple for those looking for unix time, working great in 2023

created_at="Sat May 26 09:56:04 +0000 2018"
unix_timestamp=time.mktime(time.strptime(created_at,'%a %b %d %H:%M:%S +0000 %Y'))
print(unix_timestamp)

Output: 1527342964.0

Upvotes: 0

Moniba
Moniba

Reputation: 869

Assuming your data is stored in a data frame "df" and the time for the tweet is stored in the "created_at" column. you can do:

df["created_at"] = df["created_at"].astype('datetime64[ns]') 
df["created_at"] = df.created_at.dt.to_pydatetime()

Upvotes: 2

aksappy
aksappy

Reputation: 3400

EDIT - 13 Apr 15 As suggested, please use.

 datetime.strptime('Thu Apr 23 13:38:19 +0000 2009','%a %b %d %H:%M:%S +0000 %Y').replace(tzinfo=pytz.UTC)

Now, if you want work much on the Date parsing and playing around with Date Time strings, use Babel or python-dateutil


Please ignore the suggestion below

I have not been working much on Python lately, but this should do the trick.

>>>from datetime import datetime
>>>d = datetime.strptime('Thu Apr 23 13:38:19 +0000 2009','%a %b %d %H:%M:%S %z %Y');
>>>print d.strftime('%Y-%m-%d');

This is based on Python Doc and SO

Upvotes: 8

Related Questions