Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 639
SoHei Avatar asked Oct 20 '25 04:10

SoHei


1 Answers

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.

like image 125
jlb83 Avatar answered Oct 22 '25 05:10

jlb83



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!