简体   繁体   中英

Datetime return dates between weekdays

If today is represented as follows:

todayStr = datetime.today().strftime("%d%m%Y")
todayStr

'09012021'

How do I get a list of values for:

Dates >= last Monday but < Thursday

For example today it would be:

['04012021','05012021','06012021']

Dates >= last Thursday but < Monday

Today it would be:

['07012021','08012021','09012021']

The following code:

import datetime

today = datetime.date.today()
prev_monday = today - datetime.timedelta(days=today.weekday())
dates1 = [(prev_monday + datetime.timedelta(days=d)).strftime("%d%m%Y") for d in range(3)]
dates2 = [(prev_monday + datetime.timedelta(days=d)).strftime("%d%m%Y") for d in range(3, 6)]

print(dates1)
print(dates2)

produces:

['04012021', '05012021', '06012021']
['07012021', '08012021', '09012021']

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