Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查给定日期是每月的哪个星期五?_Python_Date_Datetime - Fatal编程技术网

Python 如何检查给定日期是每月的哪个星期五?

Python 如何检查给定日期是每月的哪个星期五?,python,date,datetime,Python,Date,Datetime,我的日期如下: 2017-08-04 2017-08-18 2017-08-11 2017-08-25 datetime.date(2010, 6, 16).strftime("%V") 我想知道这个日期是那个月的哪个星期五如果月份包含五个星期五,如下图所示: 2017-06-02 // It should return 1 2017-06-09 // It should return 2 2017-06-16 // It should return 3 2017-06-23 // It s

我的日期如下:

2017-08-04
2017-08-18
2017-08-11
2017-08-25
datetime.date(2010, 6, 16).strftime("%V")
我想知道这个日期是那个月的哪个星期五如果月份包含五个星期五,如下图所示:

2017-06-02 // It should return 1
2017-06-09 // It should return 2
2017-06-16 // It should return 3
2017-06-23 // It should return 4
2017-06-30 // It should return 5
2017-08-04 // It should return 1
2017-08-18 // It should return 3
2017-08-11 // It should return 2
2017-08-25 // It should return 5
同上:

2017-06-02 // It should return 1
2017-06-09 // It should return 2
2017-06-16 // It should return 3
2017-06-23 // It should return 4
2017-06-30 // It should return 5
2017-08-04 // It should return 1
2017-08-18 // It should return 3
2017-08-11 // It should return 2
2017-08-25 // It should return 5
编辑1:我按如下方式进行了尝试:

2017-08-04
2017-08-18
2017-08-11
2017-08-25
datetime.date(2010, 6, 16).strftime("%V")
但它正在从年初开始恢复价值

d=datetime.datetime(2011, 2, 28)
(d.day-1)//7+1
星期五不算。从月初算起,这仅仅是7天

我们可以用python来做吗?

试试这段代码

import datetime

def x(y,m,d):
  d = d=datetime.datetime(y,m,d)
  firstday = d.replace(day=1)
  print(d.isocalendar()[1] - firstday.isocalendar()[1] + 1)


x(2017,8,18)

x(2017,8,25)

到目前为止,您为此做了什么?请告诉我们,您在代码的哪一部分遇到了问题,并与我们分享您对问题和可能的解决方案的想法。可能可以help@ShoaibZafar-谢谢你的帮助。这是一个很好的解决方案,退出很简单。@ShoaibZafar-它在某些日期失败,比如x(2000,01,07)。很有趣。。。正确使用
打印(int(d.strftime(“%W”))-int(firstday.strftime(“%W”))+1)
。。。同时使用正确的大小写
x(2000,1,7)