Isocalendar() Function In Python

Function Name:

isocalendar

Function Signature:

isocalendar()

 

Overview:

The instance function isocalendar() in date class returns a tuple containing

    - ISO Year

    - ISO Week Number

    - ISO Weekday

ISO standard 8601 and ISO standard 2015:

  • As per ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week. 
  • Hence, the ISO years always start on a Monday.
  • For the above reasons, ISO year can start as yearly as 29th January or as late as 4th January of a Gregorian calendar.
  • ISO years can have either 52 full weeks or 53 full weeks.
  • ISO years do not have any fractional weeks during the beginning of the year or at the end of the year.

Example:

# import the datetime module of Python

import datetime

 

gregorianDayOfWeek  = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")

isoDayOfWeek        = ("ISO Monday starts at 1","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")

 

yearCount   = 10

yearVal     = 2011

 

# Get the ISO year, ISO Week number, ISO day of the week

# for 10 new year dates in Gregorian Calendar

 

yearIndex = 0

 

print("==========================================================================")

print("Gregorian Tuple-ISO Tuple")

print("==========================================================================")

print("Year, Week Number, Day of the week - Year, Week Number, Day of the week")

print("==========================================================================")

 

while(yearIndex < yearCount):

    gregorianNewYear  = datetime.date(yearVal, 1,1)

    yearVal     = yearVal +1

    yearIndex   = yearIndex + 1

   

    gregSet = (gregorianNewYear.year,1,gregorianDayOfWeek[gregorianNewYear.weekday()])

    isoSet  = gregorianNewYear.isocalendar()

 

    print("{}-({}, {}, '{}')".format(gregSet,isoSet[0],isoSet[1],isoDayOfWeek[isoSet[2]]))

   

print("==========================================================================")

 

 

Output:

==========================================================================

Gregorian Tuple-ISO Tuple

==========================================================================

Year, Week Number, Day of the week - Year, Week Number, Day of the week

==========================================================================

(2011, 1, 'Saturday')-(2010, 52, 'Saturday')

(2012, 1, 'Sunday')-(2011, 52, 'Sunday')

(2013, 1, 'Tuesday')-(2013, 1, 'Tuesday')

(2014, 1, 'Wednesday')-(2014, 1, 'Wednesday')

(2015, 1, 'Thursday')-(2015, 1, 'Thursday')

(2016, 1, 'Friday')-(2015, 53, 'Friday')

(2017, 1, 'Sunday')-(2016, 52, 'Sunday')

(2018, 1, 'Monday')-(2018, 1, 'Monday')

(2019, 1, 'Tuesday')-(2019, 1, 'Tuesday')

(2020, 1, 'Wednesday')-(2020, 1, 'Wednesday')

==========================================================================

In the output the new year of 2011 as per ISO standards is printed as 52nd week of 2010.


Copyright 2023 © pythontic.com