简体   繁体   中英

find date based on week in month python

Is there a way to find the ranges of dates within a particular week given the year, month and week number (1st, 2nd, 3rd, etc.)? I have seen a lot of answers on how to find the week number based on a specific date but I am trying to find the date based on a week number. So for example, given (2013, 9, "Thursday", "3rd") it will give a result of (2013, 9, 19).

Here is my code so far (only works on dates that end in "teenth"):

from datetime import date

def meetupday(year, month, weekday, word):
   weekday_dic = {"Monday": 0, "Tuesday": 1, "Wednesday": 2, "Thursday": 3,
                "Friday": 4, "Saturday": 5, "Sunday": 6}
   week_of_the_month = {"1st": 1, "2nd": 2, "3rd": 3, "4th": 4, "last": 5}
   if word == "teenth":
      for day in xrange(13, 20):
        if weekday_dic[weekday] == date(year, month, day).weekday():
            return year, month, day
   else:
     for date in xrange(1, amount_of_days+1):
        date_data = datetime.date(year, month, date).isocalendar()
        if date_data[2] == weekday_dic[weekday]+1:
            return year, month, week_of_the_month[word]

This is the meetup problem from exercism.io.

My solution to find the date based on your given data would be something like that

from datetime import datetime, timedelta    
weekday_dic = {"Monday": 0, "Tuesday": 1, "Wednesday": 2, "Thursday": 3,
                   "Friday": 4, "Saturday": 5, "Sunday": 6}
week_of_the_month = {"1st": 1, "2nd": 2, "3rd": 3, "4th": 4, "last": 5}

start_first_week = first_sunday = datetime(year, month, 1)
while start_first_week.weekday() != weekday_dic[weekday]:
    start_first_week += timedelta(days=1)

start_first_week += timedelta(days=(week_of_the_month[word] - 1) * 7)

I hope that helps you a little

One quick issue I found with dada's solution was when I tried to compute the date for Memorial day in the US every year (The last Monday in May). I would get 2018-06-04. Which is not the right month based on my parameters. I added a quick fix if the output month is not matching the input, just subtract 7 days. I'm not sure what the underlying issue is though. Now returns correct answer: 2018-05-28.

from datetime import datetime, timedelta    
weekday_dic = {"Monday": 0, "Tuesday": 1, "Wednesday": 2, "Thursday": 3,
                       "Friday": 4, "Saturday": 5, "Sunday": 6}
week_of_the_month = {"1st": 1, "2nd": 2, "3rd": 3, "4th": 4, "last": 5}

start_first_week = first_sunday = datetime(year, month, 1)
while start_first_week.weekday() != weekday_dic[weekday]:
    start_first_week += timedelta(days=1)

start_first_week += timedelta(days=(week_of_the_month[word] - 1) * 7)
if start_first_week.month != month:
    start_first_week += datetime.delta(days=-7)

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