Sunday, 26 June 2016

Python: Calendar: itermonthdates(year, month)

itermonthdates(year, month)

Returns an iterator for the given month in given year. In addition to return all days for the month, it returns all days before the start of the month or after the end of the month that are required to get a complete week.

>>> from calendar import Calendar, SUNDAY
>>> cal1=Calendar()
>>> iter=cal1.itermonthdates(2015, 11)
>>> for day in iter:
...     print(day)
... 
2015-10-26
2015-10-27
2015-10-28
2015-10-29
2015-10-30
2015-10-31
2015-11-01
2015-11-02
2015-11-03
2015-11-04
2015-11-05
2015-11-06
2015-11-07
2015-11-08
2015-11-09
2015-11-10
2015-11-11
2015-11-12
2015-11-13
2015-11-14
2015-11-15
2015-11-16
2015-11-17
2015-11-18
2015-11-19
2015-11-20
2015-11-21
2015-11-22
2015-11-23
2015-11-24
2015-11-25
2015-11-26
2015-11-27
2015-11-28
2015-11-29
2015-11-30
2015-12-01
2015-12-02
2015-12-03
2015-12-04
2015-12-05
2015-12-06


Previous                                                 Next                                                 Home

Python: Calendar: iterweekdays: Get week day numbers iterweekdays()


Returns an iterator for the numbers associated for the week.
>>> import calendar
>>> cal1=calendar.Calendar(0)
>>> iter1=cal1.iterweekdays()
>>> for dayNum in iter1:
...     print(dayNum)
... 
0
1
2
3
4
5
6
>>> 
>>>
>>> 
>>> cal2=calendar.Calendar(5)
>>> iter2=cal2.iterweekdays()
>>> for dayNum in iter2:
...     print(dayNum)
... 
5
6
0
1
2
3
4



Previous                                                 Next                                                 Home

Python: Calendar: Calendar class

class calendar.Calendar(firstweekday=0)
Used to instantiate a Calendar object. Parameter firstweekday is used to specify first day of the week. By default 0 is Monday, 1 is Tuesday, ... 6 is Sunday.

Following table summarizes the methods in Calendar class.

Method
Description
iterweekdays()
Returns an iterator for the numbers associated for the week.
itermonthdates(year, month)
Returns an iterator for the given month in given year. In addition to return all days for the month, it returns all days before the start of the month or after the end of the month that are required to get a complete week.
itermonthdays2(year, month)
Returns an iterator for given month in given year. Days returned will be tuples consisting of a day number and a week day number.
itermonthdays(year, month)
Returns an iterator for given month in given year. Days returned will simply be day numbers.
monthdatescalendar(year, month)
Returns list of the weeks in given month and year as full weeks. Weeks are lists of seven datetime.date objects.
monthdays2calendar(year, month)
Returns list of weeks in given month in given year. Weeks are lists of seven tuples of day numbers and weekday numbers.
monthdayscalendar(year, month)
Returns list of weeks in given month in given year. Weeks are lists of seven day numbers.
yeardatescalendar(year, width=3)
Returns the data for given year. Returns the list of month rows. Each month row contains up to width months (defaulting to 3).
yeardays2calendar(year, width=3)
Return the data for the specified year. Entries in the week lists are tuples of day numbers and weekday numbers. Day numbers outside this month are zero.
yeardayscalendar(year, width=3)
Returns the data for given year. Entries in the week lists are day numbers. Day numbers outside this month are zero.




Previous                                                 Next                                                 Home

Python: Calendar module

Calendar module provides number of classes to provide UNIX cal functionality. All the classes provided by calendar module use Monday(0) as first day of the week and Sunday(6) as the last day of the week. You can use setfirstweekday method to set the first day of the week.

import calendar
calendar.setfirstweekday(calendar.SUNDAY)

Following are the classes provided by Calendar module.
Class
Description
Calendar
Used to create a Calendar object, By default Calendar instance use Monday as first day of the week and Sunday as the last day of the week.
TextCalendar
Used to generate plain text calendars.
HTMLCalendar
Used to generate HTML calendars.
LocaleTextCalendar
Sub class of TextCalendar, return month and weekday names in the specified locale.
LocaleHTMLCalendar
Sub class of HTMLCalendar, return month and weekday names in the specified locale.





Previous                                                 Next                                                 Home