calendar()

Creating Calendar object
import calendar
my_cal= calendar.Calendar()
Calendar options
import calendar
print(calendar.calendar(2020, w=1, l=1, c=3, m=3))
				2020

      January                February                March
Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
       1  2  3  4  5                   1  2                      1
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    2  3  4  5  6  7  8
13 14 15 16 17 18 19   10 11 12 13 14 15 16    9 10 11 12 13 14 15
20 21 22 23 24 25 26   17 18 19 20 21 22 23   16 17 18 19 20 21 22
27 28 29 30 31         24 25 26 27 28 29      23 24 25 26 27 28 29
                                              30 31

       April                   May                    June
Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
27 28 29 30            25 26 27 28 29 30 31   29 30

        July                  August               September
Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
       1  2  3  4  5                   1  2       1  2  3  4  5  6
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    7  8  9 10 11 12 13
13 14 15 16 17 18 19   10 11 12 13 14 15 16   14 15 16 17 18 19 20
20 21 22 23 24 25 26   17 18 19 20 21 22 23   21 22 23 24 25 26 27
27 28 29 30 31         24 25 26 27 28 29 30   28 29 30
                       31

      October                November               December
Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su   Mo Tu We Th Fr Sa Su
          1  2  3  4                      1       1  2  3  4  5  6
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    7  8  9 10 11 12 13
12 13 14 15 16 17 18    9 10 11 12 13 14 15   14 15 16 17 18 19 20
19 20 21 22 23 24 25   16 17 18 19 20 21 22   21 22 23 24 25 26 27
26 27 28 29 30 31      23 24 25 26 27 28 29   28 29 30 31
                       30

Present Year calendar

We can use datetime to get current year value.
from  datetime import date
current_year=date.today().year
#current_month=date.today().month

import calendar
print(calendar.calendar(current_year))

Present Year Month calendar

from  datetime import date
current_year=date.today().year
current_month=date.today().month

import calendar
print(calendar.month(current_year,current_month, w=1, l=1))
     July 2020
Mo Tu We Th Fr Sa Su
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
firstweekdayGet and set first week day of the calendar
iterweekdaysIterrator of week days
itermonthdatesdate object of the month including previous and next week
itermonthdaysdays of the month including previous and next week
itermonthdays2tuple with days of the month and week day number
itermonthdays3tuple with days , month and year numbers
itermonthdays4tuple with days , month year and week number
monthdatescalendarList of full weeks with date objects of a Month
monthdays2calendarList of weeks with day & week day as tuple for a Month
monthdayscalendarList of full weeks with 7 days numbers for a Month
yeardatescalendarList of month with full weeks with 7 date object
yeardays2calendarList of month with full weeks with 7 days tuple
yeardayscalendarList of month with full weeks with 7 days items

calendar.TextCalendar class

Methods of this class is here.
formatmonthMonthly calendar as multi-line string
prmonthPrint Monthly calendar as multi-line string
formatyearYearly calendar as multi-line string
pryearPrint yearly calendar as multi-line string

calendar.HTMLCalendar class

Methods of this class is here.
formatmonthMonthly calendar using HTML tags
formatyearYearly calendar using HTML tags
formatyearpageYearly calendar Web page using HTML tags
This module provide more functions for our use.

isleep(year)

Returns Boolean ( True or False ) based on the input year is leap year or not
import calendar
print(calendar.isleap(2012)) # True
Using for loop to check many years
import calendar
for x in range (2010,2021):
    print(x,calendar.isleap(x))
Output
2010 False
2011 False
2012 True
2013 False
2014 False
2015 False
2016 True
2017 False
2018 False
2019 False
2020 True

leapdays(year1,year2)

Number of leap years between range of input years ( year1 and year2 )
import calendar
print(calendar.leapdays(2010,2021)) # 3

weekday(year,month,day)

Returns the number of week day of the input date ( in year , month and day format ). It returns 0 for Monday, 1 for Tuesday ... and 6 for Sunday.
import calendar
print(calendar.weekday(2010,12,31))  # 4

weekheader(n)

Returns header of abbreviated weekday names. n here is the width of chars for each weekday name to be used. By using n value as 1, 2,3,9 we can get different abbreviated names of the weekdays.
import calendar
print(calendar.weekheader(1)) # use 9,3,2,1
Output
M T W T F S S
print(calendar.weekheader(9))
Output
Monday   Tuesday  Wednesday  Thursday   Friday   Saturday   Sunday

monthrange(year,month)

Returns a tuple having weekday number of first day of the month and number of days in the month. Inputs are year and month for which details are returned.
import calendar
print(calendar.monthrange(2020,7)) # (2,31)
For month July of year 2020 , the weekday number for first day of the month is 2 ( Wednesday ) and there are 31 days in the month.
import calendar
x=calendar.monthrange(2020,7)
print("First weekday:",x[0], ", Total Days ",x[1])
Output
First weekday: 2 , Total Days  31

monthcalendar(year,month)

Returns a matrix having each week as row. Days outside the month are returned as 0.
import calendar
print(calendar.monthcalendar(2020,7))
Output
[[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 0, 0]]
By default the week starts with Monday. Can be changed by using setfirstweekday
import calendar
calendar.setfirstweekday(3)
print(calendar.monthcalendar(2020,7))
Output
[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 31, 0, 0, 0, 0, 0]]

prcal(year, w=0, l=0, c=6, m=3)

print year calendar as returned by calendar() ( check code above )

timegm(tuple)

tuple is the input year, month, day and time. We get timestamp assuming an epoch of 1970 as output.
Get all details about Timestamp and generate timestamp
by using Calendar and time sliders.
import calendar
print(calendar.timegm((2020,7,30,12,12,35)))
Output
1596111155
By using above timestamp we can get the correspoding date and time. We will use fromtimestamp()
import calendar
x=calendar.timegm((2020,7,30,12,12,35))

from datetime import datetime

print("Present Timestamp : ",x)
dt=datetime.fromtimestamp(x)
print(dt)
Output ( + 5:30 for localtime )
Present Timestamp :  1596111155
2020-07-30 17:42:35

calendar.month_name

Array of list of local month names
x=calendar.month_name
for i in x:
    print(i,' ',end='')
January  February  March  April May  June 
July  August September  October  November  December 

calendar.month_abbr

Array of list of local abbreviated months
x=calendar.month_abbr
for i in x:
    print(i,' ',end='')
Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec 

calendar.day_name

Array of list of day names
x=calendar.day_name
for i in x:
    print(i,' ',end='')
Monday  Tuesday  Wednesday  Thursday  Friday  Saturday  Sunday

calendar.day_abbr

Array of list of day abbreviated names
x=calendar.day_abbr
for i in x:
    print(i,' ',end='')
Mon  Tue  Wed  Thu  Fri  Sat  Sun

All Built in Functions in Python
max() Bitwise operators using hash() int() float()



plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer