Im working with the Python Google Calendar APi.
The Code:
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()
If run, the output looks like this:
2018-12-26T10:00:00+01:00 Event Name
That is ISO format from what I know. The problem is that I cannot find a way of formatting that time to something human like "26 December, 10:00".
I've tried alot of things.
I cannot use .strftime(), .strptime()
nordateutil.parser
The most promising this was doing: variable = datetime.datetime.strptime(now, '%Y-%m-%d %H:%M:%S.%f').strftime("%B %d, %Y") but then I get this error..
ValueError: time data '2018-12-13T11:56:31.095470Z' does not match format '%Y-%m-%d %H:%M:%S.%f'
I've searched on the internet but I've found just an unanswered question