S.Rakin
S.Rakin

Reputation: 832

How to determine the date of Starting day of current and last week in python ?

I need to find the date of the starter day of current and last week.

Here, business day of a week starts from SUNDAY. So, for today the date I want is to be "07-16-2017" as "mm--dd--yyyy" format. I can get today's date easily from datetime.datetime.now().strftime ("%Y-%m-%d") but from the sysdate I have to pull out the starter day of the week.

I need the starter day's date for last week as well.

There is no default method for week information in datetime. Is there any other method in any package in python to determine the required information ?

Upvotes: 4

Views: 289

Answers (1)

maestromusica
maestromusica

Reputation: 706

You can use calendar.firstweekday() to check what the first day of the week is on that computer (0 is Monday, 6 is Sunday).

1) Let's say that firstweekday returned 1 (Sunday). Then you can use

date.today() - datetime.timedelta(days=date.today().isoweekday() % 7)

to compute the date of the last Sunday.

2) Let's say that firstweekday returned 0 (Monday).

date.today() - datetime.timedelta(days=date.today().isoweekday() % 7 - 1)

to compute the date of the last Monday.

Hope this gives you some direction.

Upvotes: 2

Related Questions