简体   繁体   English

根据月份中的周查找日期python

[英]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). 因此,例如,给定(2013,9,“ Thursday”,“ 3rd”),它将得出(2013,9,19)的结果。

Here is my code so far (only works on dates that end in "teenth"): 到目前为止,这是我的代码(仅适用于以“ teen”结尾的日期):

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. 这是来自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). dada解决方案的一个快速问题是,当我尝试计算每年美国的阵亡将士纪念日的日期(5月的最后一个星期一)时。 I would get 2018-06-04. 我会得到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. 如果输出月份与输入不匹配,我添加了一个快速修复,只需减去7天即可。 I'm not sure what the underlying issue is though. 我不确定潜在的问题是什么。 Now returns correct answer: 2018-05-28. 现在返回正确答案: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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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