I'm trying to use the Google Calendar API for python and want to change the format of the date it's outputting. I've tried using dateutil and strftime, but couldn't get it to work.....
Right now it's outputting yyyy-mm-dd hh:mm:ss-hh:mm "Event Name".
I want it to just display either yyyy-mm-dd, or in a format like "Apr 15, 2018".
Thank you and much appreciated!
"""
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()