我正在使用Python Google日历APi。在
代码:
import datetime
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('calendar', 'v3', http=creds.authorize(Http()))
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
if __name__ == '__main__':
main()
如果运行,输出如下所示:
^{pr2}$据我所知,这是ISO格式。问题是,我找不到一种方式来将时间格式化为像“12月26日,10:00”这样的人。在
我试过很多东西。
我不能使用.strftime(), .strptime()
或dateutil.parser
最有希望的是: 变量=日期时间.日期时间.strptime(现在,'%Y-%m-%d%H:%m:%S.%f')。strftime(“%B%d,%Y”),但是我得到了这个错误。。在
值错误:时间数据“2018-12-13T11:56:31.095470Z”与格式“%Y-%m-%d%H:%m:%S.%f”不匹配
我在网上搜索过,但只找到一个unanswered question
您可以使用
dateutil.parser
和datetime.datetime.strftime
一起使用。输出:
^{pr2}$然后使用下面的命令打印事件。
^{3}$相关问题 更多 >
编程相关推荐