Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To my knowledge, there is no direct way to get the day of an event in the Calendar API. If the date format of your results is as it is in your parameters (<code>2012-12-24T00:00:00Z</code>), you can use string formatting in combination with the <code>datetime</code> module. Here, the <code>%A</code> is the string formatting parameter that returns the day of the week of the datetime object defined by running <code>strptime</code> on our string with the corresponding format:</p> <pre><code>In [1]: from datetime import datetime In [2]: s = '2012-12-24T00:00:00Z' In [3]: d = datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ') In [4]: '{0:%A}'.format(d) Out[4]: 'Monday' </code></pre> <p>And as a function:</p> <pre><code>In [8]: def createDayName(s): ...: d = datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ') ...: return '{0:%A}'.format(d) ...: In [9]: createDayName('2012-12-24T00:00:00Z') Out[9]: 'Monday' In [10]: createDayName('2012-12-30T23:59:59Z') Out[10]: 'Sunday' </code></pre> <p>Building on that, if you need to handle multi-day events, you can try something like this, where the main piece involves <code>timedelta</code> and iterating the number of days between the two events (note that this is a bit arbitrary, but will hopefully provide a useful example):</p> <pre><code>from datetime import datetime, timedelta # This structure will allow us to append to our dictionary without # there needing to be a key first (comes in handy) from collections import defaultdict def days_in_range(start, end, daysDict): # Convert your start/end dates start_d = datetime.strptime(start, '%Y-%m-%dT%H:%M:%SZ') end_d = datetime.strptime(end, '%Y-%m-%dT%H:%M:%SZ') # Now iterate over the days between those two dates, adding # an arbitrary value to the 'day' key of our dict for i in range((end_d - start_d).days + 1): day_name = '{0:%A}'.format(start_d + timedelta(days=i)) daysDict[day_name].append(i) return daysDict # Create your dictionary that will have a list as the default value daysDict = defaultdict(list) start = '2012-12-24T00:00:00Z' end = '2012-12-30T23:59:59Z' # I would probably reevaluate this part, but the reason for # passing the dictionary itself to the function is so that # it can better fit into situations where you have multiple events # (although a class structure may be well-suited for this, but # that may be overcomplicating things a bit :) ) daysDict = days_in_range(start, end, daysDict) for day, value in daysDict.iteritems(): print day, value </code></pre> <p>This prints the following (as dictionaries are inherently unordered, it may appear different for you):</p> <pre><code>Monday [0] Tuesday [1] Friday [4] Wednesday [2] Thursday [3] Sunday [6] Saturday [5] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload