Isocalendar() Method Of Datetime Class In Python

Method Name:

isocalendar

 

Method Signature:

isocalendar()

 

Overview:

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

 

  • ISO Year

 

  • ISO Week Number

 

  • ISO Weekday

 

  • As per ISO standard 8601 and ISO standard 2015, an ISO week has Thursday as the middle of the week. This means a new ISO year can start as early as 29th January or as late as 4th January of a regular calendar i.e, a Gregorian calendar.

 

  • Since, an ISO year perfectly starts on a Monday - ISO years have either complete 52 weeks duration or 53 weeks duration.

 

  • There are no fractional weeks for an ISO year.

 

  • The sample program below, prints New Year dates for two years starting from 2020 in Gregorian Calendar as well as in ISO Calendar.

 

Example:

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()))

 

Output:

Regular Date:2020-01-01 00:00:00

ISO Date:(2020, 1, 3)

Regular Date:2021-01-01 00:00:00

ISO Date:(2020, 53, 5)

 

 


Copyright 2023 © pythontic.com