Home > Net >  Output the Tuesday 6 weeks in the future in Python?
Output the Tuesday 6 weeks in the future in Python?

Time:05-08

UPDATE: post edited to add answer to end of post

Core Question

Using Python, how do I output the date of the Tuesday that occurs 6 weeks after a certain date range?

Context

I work at a SaaS company in a customer facing role. Whenever I do an implementation for a client, the client receives a survey email on the Tuesday that occurs in the 6th week after our initial interaction.

To know which Tuesday to be extra nice on, we currently have to reference a chart that says if the interaction falls in date range x, then the client receives their survey solicitation on Tuesday y.

An example of this would be: if the interaction happened sometime within Apr. 18 - Apr. 22, then the survey goes out on May 31.

I would prefer for this to be done without having to hard code the date ranges and their corresponding Tuesdays into my program (just because I'm lazy and don't want to update the dates manually as the months go by), but I'm open to that solution if that's how it has to be. :)

Code Attempt

I can use datetime to output a particular date x weeks from today's date, but I'm not sure how to get from here to what I want to do.

import time
from datetime import datetime, timedelta

time1 = (time.strftime("%m/%d/%Y")) #current date

time2 = ((datetime.now()   timedelta(weeks=6)).strftime('%m/%d/%Y')) #current date   six weeks

print(time1)

print((datetime.now()   timedelta(weeks=6)).strftime('%m/%d/%Y'))

Disclaimer: I am a beginner and although I did search for an answer to this question before posting, I may not have known the right terms to use. If this is a duplicate question, I would be thrilled to be pointed in the right direction. :)


~~~UPDATED ANSWER~~~

Thanks to @Mandias for getting me on the right track. I was able to use week numbers to achieve my desired result.

from datetime import datetime, timedelta, date

today = date.today() #get today's date
todays_week = today.isocalendar()[1] #get the current week number based on today's date
survey_week = todays_week   6 #add 6 weeks to the current week number
todays_year = int(today.strftime("%Y")) #get today's calendar year and turn it from a str to an int
survey_week_tuesday = date.fromisocalendar(todays_year, survey_week, 2)  #(year, week, day of week) 2 is for Tuesday


print("Current Week Number:")
print(todays_week)

print("Current Week Number   6 Weeks:")
print(todays_week   6)

print("Today's Year:")
print(todays_year)

print("The Tuesday on the 6th week from the current week (i.e. survey tuesday):")
print(survey_week_tuesday.strftime('%m-%d-%Y')) #using strftime to format the survey date into MM-DD-YYYY format because that's what we use here even though DD-MM-YYYY makes more sense

CodePudding user response:

I believe what you're looking for is shown in the example code below:

from datetime import datetime, timedelta

# Establish your date range
start = datetime.strptime("12-17-2010", "%m-%d-%Y")
end = datetime.strptime("1-05-2011", "%m-%d-%Y")
elapsed_days = (end-start).days

# Get each day in that range offset by 6 weeks
# You may need to adjust the elapsed_days value
offset = 6 #weeks
offset_days = [start timedelta(weeks=offset, days=i) for i in range(elapsed_days)]
print(offset_days)
  • Related