116

I am required to extract the time of the day from the datetime.datetime object returned by the created_at attribute, but how can I do that?

This is my code for getting the datetime.datetime object.

from datetime import *
import tweepy

consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
tweets = tweepy.Cursor(api.home_timeline).items(limit = 2)
t1 = datetime.strptime('Wed Jun 01 12:53:42 +0000 2011', '%a %b %d %H:%M:%S +0000 %Y')
for tweet in tweets:
   print (tweet.created_at - t1)
   t1 = tweet.created_at

I need to only extract the hour and minutes from t1.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhishek Sharma
  • 1,909
  • 2
  • 15
  • 24

5 Answers5

151

I don't know how you want to format it, but you can do:

print("Created at %s:%s" % (t1.hour, t1.minute))

for example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
afrendeiro
  • 2,423
  • 2
  • 23
  • 22
  • 1
    An explanation would be in order. What is it supposed to do? How does it work? What features of Python does it use? Can you link to documentation? – Peter Mortensen Jan 26 '22 at 04:49
55

If the time is 11:03, then afrendeiro's answer will print 11:3.

You could zero-pad the minutes:

"Created at {:d}:{:02d}".format(tdate.hour, tdate.minute)

Or go another way and use tdate.time() and only take the hour/minute part:

str(tdate.time())[0:5]
shaneb
  • 1,314
  • 1
  • 13
  • 18
36
import datetime
    
YEAR        = datetime.date.today().year     # the current year
MONTH       = datetime.date.today().month    # the current month
DATE        = datetime.date.today().day      # the current day
HOUR        = datetime.datetime.now().hour   # the current hour
MINUTE      = datetime.datetime.now().minute # the current minute
SECONDS     = datetime.datetime.now().second #the current second
    
print(YEAR, MONTH, DATE, HOUR, MINUTE, SECONDS)
2021 3 11 19 20 57
hariK
  • 2,722
  • 13
  • 18
24

It's easier to use the timestamp for these things since Tweepy gets both:

import datetime
print(datetime.datetime.fromtimestamp(int(t1)).strftime('%H:%M'))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
11

datetime has fields hour and minute. So to get the hours and minutes, you would use t1.hour and t1.minute.

However, when you subtract two datetimes, the result is a timedelta, which only has the days and seconds fields. So you'll need to divide and multiply as necessary to get the numbers you need.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Liam Kirsh
  • 111
  • 3
  • Re *"which only has the days and seconds fields"*: The [documentation says](https://docs.python.org/3/library/datetime.html#timedelta-objects) *"Only days, seconds and microseconds are stored internally"* (also microseconds). What is the explanation? As the OP seems to have left the building, perhaps somebody can chime in? – Peter Mortensen Jan 29 '22 at 20:46