What is isocalendar in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The isocalendar() function is used to return a tuple containing ISO Year, ISO Week Number, and ISO Weekday.

    Note:

    • According to ISO standard 8601 and ISO standard 2015, Thursday is the middle day of a week.
    • Therefore, ISO years always start with Monday.
    • 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.

    Syntax: isocalendar()

    Parameters: This function does not accept any parameter.

    Return values: This function returns a tuple of ISO Year, ISO Week Number and ISO Weekday.

    Example 1: Get  ISO Year, ISO Week Number, and ISO Weekday.

    Python3

    from datetime import date

    Todays_date = date.today()

    print(Todays_date)

    print(Todays_date.isocalendar())

    Output:

    2021-07-25
    (2021, 29, 7)

    Example 2:  Get ISO Year, ISO Week Number, and ISO Weekday with a specific date.

    Python3

    from datetime import date

    A = date(2020, 10, 11)

    Date = A.isocalendar()

    print("Original date:",A)

    print("Date in isocalendar is:", Date)

    Output:

    Original date: 2020-10-11
    Date in isocalendar is: (2020, 41, 7)

    isocalendar() method of datetime class returns a tuple with the following elements

    import datetime

    # 2020 new year

    dateTimeInstance = datetime.datetime(2020, 1, 1, 0, 0, 0)

    print("Regular Date:{}".format(dateTimeInstance))

    print("ISO Date:{}".format(dateTimeInstance.isocalendar()))

    # 2021 new year

    dateTimeInstance = dateTimeInstance.replace(year=2021)

    print("Regular Date:{}".format(dateTimeInstance))

    print("ISO Date:{}".format(dateTimeInstance.isocalendar()))

    # 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("==========================================================================")

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

    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.

    What is Isocalendar?

    The calendar defined in the ISO standards 8601 is commonly referred to as the ISO calendar. The ISO calendar corresponds with the Gregorian (Western) calendar and uses the same year number, but its length is defined to be an integral number of weeks.

    How do I use Isocalendar in Python?

    # Import the date module from datetime. from datetime import date..
    # Call the today() method that returns today's date. today = date. today().
    # Print today's date. print("Today's date=", today).
    # to get its ISO Year, Week Number and Weekday. print('ISO notation: (Year, Week Number, Week Day)=', today. isocalendar()).

    What is ISO week in Python?

    The isoweek module provide the class Week. Instances represent specific weeks spanning Monday to Sunday. There are 52 or 53 numbered weeks in a year. Week 1 is defined to be the first week with 4 or more days in January. It's called isoweek because this is the week definition of ISO 8601.

    How do I get the day of the week in Python?

    Use the weekday() method The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday.