gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
python how to get n work days ago
- User
python how to get n work days ago
- Assistant
To get the date of n working days ago in Python, you can use the
datetimemodule and a loop to subtract the required number of working days.Here is a function that calculates the date of n working days ago:
from datetime import datetime, timedelta def get_n_work_days_ago(n): today = datetime.today() work_days = 0 delta = timedelta(days=1) while work_days < n: today -= delta if today.weekday() < 5: # Assuming Monday to Friday are considered working days work_days += 1 return today.strftime("%Y-%m-%d")You can use the function like this:
n = 10 work_days_ago = get_n_work_days_ago(n) print(f"{n} work days ago was: {work_days_ago}")This will give you the date of
nworking days ago.