如何从datetime.datetime对象提取小时和分钟? [英] How to extract hours and minutes from a datetime.datetime object?
本文介绍了如何从datetime.datetime对象提取小时和分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从created_at属性返回的datetime.datetime对象中提取一天中的时间。但是我不知道该怎么做。这是我获取datetime.datetime对象的代码。
from datetime 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')
用于发推文:
print(tweet.created_at-t1)
t1 = tweet.created_at
我只需要提取t1的小时和分钟。
解决方案
不知道您要如何设置其格式,但是您可以这样做:
print(创建于%s:%s%(t1.hour,t1.minute))
例如,pre>
。
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
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))
for example.
这篇关于如何从datetime.datetime对象提取小时和分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文