pythondatetime

How can I find week numbers with weeks starting on Sunday in Python?


Total newbie to Python, so apologies in advance if this is obvious. I have a number of datetime values, for which I would like to identify a year and week number, for example:

start_date = datetime.datetime(2015,1,26,0,0,0)

The catch is, I would like the week to be defined as starting on a Sunday.

I understand that the datetime.isocalendar() can get me week numbers, but they will be calculated under the assumption that weeks start on Mondays. Is there any other way?

Edit: Running Python 2.7.6

Thank you!


Solution

  • String format %U will give the week of the year counting from Sunday:

    t1 = datetime.datetime.now()
    t1.strftime("%U")
    

    See http://strftime.org/ for full list of format parameters.