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

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

问题描述

我需要从 created_at 属性返回的 datetime.datetime 对象中提取一天中的时间.但我不明白该怎么做.这是我获取 datetime.datetime 对象的代码.

I am required to extract the time of the day from the datetime.datetime object returned by the created_at attribute. But I do not understand how to 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

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

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

推荐答案

不知道你想如何格式化它,但你可以这样做:

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

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

例如.

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

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