5

I'm trying to get week number with this simple script on python.

import datetime

t = datetime.date(2013,8,18)
print t.isocalendar()[1]

It returns 33 for ISO format, but for the US calendar it should be 34. How can I get this week number for US format?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Did you see [How to get week number in Python?](http://stackoverflow.com/a/2608868)? – Martijn Pieters Aug 18 '13 at 15:39
  • he want not just week number, he want US format week number. – Michael Aug 18 '13 at 15:52
  • Possible duplicate of [Python North American work week number from datetime?](https://stackoverflow.com/questions/55658435/python-north-american-work-week-number-from-datetime) – pyjamas Jul 19 '19 at 01:24

1 Answers1

0

I ran into the same problem. The ISO calendar is a great concept and may have some really good uses, but for business in the U.S. it just doesn't work well. I can't write an application that could potentially report days in the end of December as being in the next calendar year. The solution that works for me is:

from datetime import *
today = datetime.today()
print today.strftime("%U")

This will return the correct U.S. week number. The only caveat with this is that all days in a new year preceding the first Sunday are considered to be in week 0. Credit to Chaggster who gave a similar answer here: How to get week number in Python?

Community
  • 1
  • 1
VT_Drew
  • 374
  • 5
  • 10
  • This answer gives incorrect results. The two answers here are correct: https://stackoverflow.com/questions/55658435/python-north-american-work-week-number-from-datetime – pyjamas Jul 19 '19 at 01:21
  • In the image below I compared many different methods and highlighted the two columns which match the North American system of work week numbering. You can see %U does not. https://www.screencast.com/t/S6S7ufarnS9T Here is the code to replicate this: https://pastebin.com/QdtgUE5N – pyjamas Jul 19 '19 at 01:22