简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM