Search code examples
pythonpython-3.xtweepytwitterapi-python

Using Tweepy, is there a way to be able to interact with the UTC time that 'created_at' returns?


In my case, I want to be able to get what's returned by 'created_at' and use it in an if statement or loop or anything else.

mentions = api.mentions_timeline(tweet_mode="extended")

for tweet in reversed(mentions):
    created_at_id = tweet.id 
    created_at = tweet.created_at 
    print(f"tweet was made at {created_at}")

tweet was made at 2021-05-14 00:22:23 # This is what is printed

This is fine and dandy, the time is returned in UTC format but what if I wanted to compare the time and date (just time if possible) and use it in a statement?

if *hour of the tweet* 12 hours before datetime.datetime.now():
    print("yes")
# Some pseudocode here because I still have to figure out how to be able to compare the current date

So the question stands, how could I access the time of the tweet so I can use it in a statement? Any help would be greatly appreciated!


Solution

  • Let me know if this is what you're looking for. If not, I will correct it.

    from dateutil.parser import parse
    from datetime import datetime, timedelta
    
    twelve_hours_before = (datetime.now() - timedelta(hours=12)).strftime('%Y-%m-%d %H:%M:%S')
    time_string = '2021-05-14 23:35:23'
    
    # use your datetime object for parse(time_string).
    if parse(twelve_hours_before) <= parse(time_string): 
        print('yes')