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...
Python provides the strptime() method, in its datetime class, to convert a string representation of the date/time into a date object.
When you choose a date and time (ex. Fri Nov 3 6:55 PM) the date on the post will have the day be the final two digits of the time (ex. 11/55/17).
For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt. date from Pandas in Python.
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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With