Google日历API日期时间格式python

Google日历API日期时间格式python,python,string,api,datetime,google-calendar-api,Python,String,Api,Datetime,Google Calendar Api,我正在尝试使用Google日历API for python,并希望更改其输出日期的格式。我尝试使用dateutil和strftime,但无法使其正常工作 现在它正在输出yyyy-mm-dd hh:mm:ss hh:mm“事件名称” 我希望它只显示yyyy-mm-dd,或者以类似“2018年4月15日”的格式显示 谢谢,非常感谢 """ Shows basic usage of the Google Calendar API. Creates a Google Calendar API servi

我正在尝试使用Google日历API for python,并希望更改其输出日期的格式。我尝试使用dateutil和strftime,但无法使其正常工作

现在它正在输出yyyy-mm-dd hh:mm:ss hh:mm“事件名称”

我希望它只显示yyyy-mm-dd,或者以类似“2018年4月15日”的格式显示

谢谢,非常感谢

"""
Shows basic usage of the Google Calendar API. Creates a Google Calendar API
service object and outputs a list of the next 10 events on the user's calendar.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import datetime
import time

# Setup the Calendar API
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.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', [])

outFile = open('sample.txt' , 'w')

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'])

    outFile.write(str(event['summary']))
    outFile.write('  ')
    outFile.write(start)
    outFile.write('\n')
outFile.close()

我假设
start
变量包含要打印的
datetime
对象。如果是这样,您可以通过调用
strftime
(想想:stringfromtime)来格式化它。从datetime对象返回字符串:

date_format = '%Y-%m-%d'
print(start.strftime(date_format), event['summary'])

文档中包含一个表格,解释了各种格式选项

您可能已经找到了此问题的解决方案,但却将我带到了这个“未回答”的问题

同样的答案

您可以同时使用
dateutil.parser
datetime.datetime.strftime
完成此操作。

from dateutil.parser import parse as dtparse
from datetime import datetime as dt

start = '2018-12-26T10:00:00+01:00'   # Let's say your start value returns this as 'str'
tmfmt = '%d %B, %H:%M %p'             # Gives you date-time in the format '26 December, 10:00 AM' as you mentioned

# now use the dtparse to read your event start time and dt.strftime to format it
stime = dt.strftime(dtparse(start), format=tmfmt)
输出:

Out[23]: '26 December, 10:00 AM'
print(stime, event['summary'])
outFile.write("{}\t{}\n".format(str(event['summary']), stime)
然后使用下面的命令打印事件或将4个outfile.write命令组合为一个,按如下方式写入文件:

Out[23]: '26 December, 10:00 AM'
print(stime, event['summary'])
outFile.write("{}\t{}\n".format(str(event['summary']), stime)

对于那些希望使用datetime格式将Python的datetime转换为Google的API格式的人,可以使用以下方法:

datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')

我得到错误:AttributeError:“unicode”对象没有属性“strftime”我必须安装strftime吗?可能strftime不起作用,因为我的代码的输出是一个时间范围,而不仅仅是一个特定的日期和时间,可能与