非标准周编号

2024-04-23 08:40:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的大学时间表网站构建一个API。默认选项是查找今天的时间表。然而,时间表系统使用的周编号与我们通常使用的不同。你知道吗

我需要的两件事之一,建立网址是周数。你知道吗

TIMETABLE_URL = 'http://timetable.ait.ie/reporting/textspreadsheet;student+set;id;{}?t=student+set+textspreadsheet&days=1-5&=&periods=3-20&=student+set+textspreadsheet&weeks={}&template=student+set+textspreadsheet'

周编号应从以下日期开始:27 Aug 2018 - 2 Sep 2018。在此之后,week 2将是3 Sep 2018-9 Sep 2018,依此类推。这应该延续到新的一年里,31 Dec 2018-6 Jan 2019的日期应该是week 19。今年总共有52周。你知道吗

我知道如何检查某个日期是否在上面的某个范围之间,但我希望避免手动设置所有日期范围。我怎么能有一个脚本知道,例如,它在9月12日第3周?你知道吗


Tags: api网站系统选项时间表student两件事sep
1条回答
网友
1楼 · 发布于 2024-04-23 08:40:11

使用datetime.datetime对象:

from datetime import datetime
start = datetime.strptime('20180827', '%Y%m%d')
current = datetime.strptime('20180912', '%Y%m%d')
print((current - start).days//7 +1) # The week number
# Output: 3

这也可以处理不同的年份。请注意,这仅在开始日期为星期一时有效。你知道吗

相关问题 更多 >