如何从datetime.datetime对象中提取小时和分钟? [英] How can I extract hours and minutes from a datetime.datetime object?

查看:104
本文介绍了如何从datetime.datetime对象中提取小时和分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从created_at属性返回的datetime.datetime对象中提取一天中的时间,但我如何才能做到这一点?

这是我用于获取datetime.datetime对象的代码。

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

我只需要从t1中提取小时和分钟。

推荐答案

我不知道您要如何格式化它,但您可以这样做:

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

例如。

这篇关于如何从datetime.datetime对象中提取小时和分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆