Kapil Jain
Kapil Jain

Reputation: 188

Get next and last ISO week number in a year

By this I am getting current week number

datetime.date(2014, 9, 30).isocalendar()[1]

but I want to get what is next ISO week number and max ISO week number in current year in python.

I can't add current week number + 1 to get next week number, because it may be that year doesn't have that week number.

Upvotes: 0

Views: 1846

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 121966

To get next week's week number, add a timedelta:

>>> import datetime
>>> today = datetime.date(2014, 9, 30)
>>> next_week = today + datetime.timedelta(days=7)
>>> next_week.isocalendar()[1]
41

To get the last week number in the year, note that the following rule is used:

The following years have 53 weeks:

  • years starting with Thursday
  • leap years starting with Wednesday

Upvotes: 1

Related Questions