1

I have a requirement to run a report on a weekly basis with week starting from Saturday and ending on Friday. However since the datetime or calendar module has week starting from Monday i couldn't use WEEKDAY option.

I tried the below option however it still gives me 5 for saturday,is there any options available to set weekstart day to Saturday so that for saturday it is 0 ? so that i can minus it from current date to get the desired dates or any other solution to acheive the same

Eg) If i run the report on August 30th it should fetch the data for August 18 to August 24

import calendar
calendar.setfirstweekday(calendar.SATURDAY)
calendar.firstweekday()
5
Rajeev
  • 1,031
  • 2
  • 13
  • 25
  • 1
    You haven't really told us what you want to produce. It sounds like you should specify a function. That means specifying what its input should be and what its output should be. – Denziloe Sep 01 '18 at 00:12
  • I want to make a week to beign from saturday currently it begins from Monday. I.e Sat-0,Sun-1,Mon-2,Tue-3,Wed-4,Thur-5,Fri-6 instead of Mon -0,Tue-1,Wed-2,Thur-3,Fri-4,Sat-5,Sun-6 – Rajeev Sep 01 '18 at 23:40

2 Answers2

4

Thanks to the post Python: give start and end of week data from a given date slightly modified it based on my needs

dt = datetime.now()
start = (dt - timedelta(days = (dt.weekday() + 2) % 7))  - timedelta(days=7)
end = (start + timedelta(days=6))
print(start.strftime("%Y-%m-%d"))
print(end.strftime("%Y-%m-%d"))
Rajeev
  • 1,031
  • 2
  • 13
  • 25
0

It gives you 5 for Saturday, because 0 is Monday.

>>> calendar.setfirstweekday(calendar.MONDAY)
>>> calendar.firstweekday()
0
Patrick
  • 2,044
  • 1
  • 25
  • 44
  • Is there a way to make saturday as 0? – Rajeev Sep 03 '18 at 06:43
  • Why would that matter? I don't understand the question. Technically, yes you could probably override everything, but I don't see why you'd need to do that. – Patrick Sep 04 '18 at 19:24