I am implementing tweepy api in my project and I am trying to get the time a tweet was created using tweet.created_at . It returns a set of Number instead of the actual date string that is shown in the Official twitter API docs. below is the code to get the data:
for tweet in tweepy.Cursor(tweet_api.search, q=user, count=200,
lang="en").items(4000):
if re.match(pattern, tweet.text):
continue
tweet_d.append(tweet)
data["text"] = [clean_tweet(tweet.text) for tweet in tweet_d]
data["len"] = [len(clean_tweet(tweet.text)) for tweet in tweet_d]
data["Date"] = [tweet.created_at for tweet in tweet_d]
data['Source'] = [tweet.source for tweet in tweet_d]
data['Likes'] = [tweet.favorite_count for tweet in tweet_d]
data['RTs'] = [tweet.retweet_count for tweet in tweet_d]
Here is the Array object printed out:
[ {"text":"b see as you fresh like todays boli", "len":35, "Date":1588602936000, "Source":"Twitter for Android", "Likes":0, "RTs":0}, {"text":"b", "len":1, "Date":1588598786000, "Source":"Twitter for Android", "Likes":1, "RTs":0}, {"text":"b", "len":1, "Date":1588597919000, "Source":"Twitter for Android", "Likes":1, "RTs":0},]
I am trying to get the Date as standard datetime("Wed May 23 06:01:13 +0000 2007",) instead of those set of Numbers shown(1588598786000).
The number is an UNIX timestamp in milliseconds.
Divide by 1000 to get to seconds, then pass to datetime.datetime.fromtimestamp()
:
>>> import datetime
>>> t = datetime.datetime.fromtimestamp(1588602936000 / 1000)
datetime.datetime(2020, 5, 4, 17, 35, 36)
>>> t.isoformat()
'2020-05-04T17:35:36'