我正在尝试获取一个给定周内的日期列表。 我可以得到一年和一周。
E.g:
>>> year, week, dow = datetime.today().isocalendar()
>>> week
>>> 39
我想在第39周获得7天。今年,2015年,我会得到
[21, 22, 23, 24, 25, 26, 27]
如图所示here
注意
我发现很多问题要求:
这不是重复,我不需要过去7天,我已经有了当前的周数。
我需要在给定的一周内获得7天,在这种情况下,第39周。感谢您的时间。
答案 0 :(得分:1)
isocalendar
的难点在于它并不是真正计算每月的日期。因此,你必须翻译回来才能得到它。 strptime
可以提供帮助:
year, week, dow = datetime.today().isocalendar()
result = [datetime.strptime(str(year) + "-" + str(week-1) + "-" + str(x), "%Y-%W-%w").day for x in range(1,7)]
我们在这里做的是构建一个striptime
可以理解的字符串,从一周后开始(计算从0到1的计数)并从一周开始(星期一,即{ {1}})并在7天内构建每天的日期时间。
通过在这两个陈述之间使用1
(添加或删除几周来实现月份休息),我们可以看到它有效:
week
现在,为了解决评论中提到的两个非常现实的问题,我们必须稍微修改一下:
>>> year, week, dow = datetime.today().isocalendar()
>>> result = [datetime.strptime(str(year) + "-" + str(week-1) + "-" + str(x), "%Y-%W-%w").day for x in range(1,7)]
>>> result
[21, 22, 23, 24, 25, 26]
>>> year, week, dow = datetime.today().isocalendar()
>>> week = week + 1
>>> result = [datetime.strptime(str(year) + "-" + str(week-1) + "-" + str(x), "%Y-%W-%w").day for x in range(1,7)]
>>> result
[28, 29, 30, 1, 2, 3]
这使用year, week, dow = datetime.today().isocalendar()
week_start = datetime.strptime(str(year) + "-" + str(week-2) + "-0", "%Y-%W-%w")
result = [(week_start + timedelta(days=x)).day for x in range(0,7)]
来增加。为了完成这项工作,我们必须在整个周期间进行备份(因此timedelta
代替-2
)。然后,当我们遍历整周时,for comprehension增加了越来越大的时间增量:
-1
答案 1 :(得分:1)
当然可以改进,但看起来似乎......
def days_of_the_current_numbered_week():
import datetime
import calendar
# dictionary of days of the week
days = { 0 : "Sunday",
1 : "Monday",
2 : "Tuesday",
3 : "Wednesday",
4 : "Thursday",
5 : "Friday",
6 : "Saturday",
7 : "Sunday" }
allYearDates = []
w_days_numbers = []
# today
now = datetime.datetime.now()
# get current values for year, mon, day
year = int(now.year)
mon = int(now.month)
day = int(now.day)
# get this week number
# thisWeekN = datetime.date(year, mon, day).isocalendar()[1]
thisWeekN = datetime.datetime.utcnow().isocalendar()[1]
# get Calendar obect
c = calendar.Calendar()
# get all the days for this year in a list
for i in range(1, 13):
for d in c.itermonthdays2(year, i):
allYearDates.append(d)
# in the first seven days could be 0's as days, to continue week #'s
# these tuples need to be removed to produce accurate mapping between
# weeks and 7 days chunks
first_seven = allYearDates[:7]
no_zeros = [ d for d in first_seven if d[0] != 0]
allYearDates = no_zeros + allYearDates[8:]
# divid all days of year list into one week chuncks
lt7 = listChunks(allYearDates,7)
# get days for this week number
thisWeekDays = lt7[thisWeekN]
# remove right part of the days tuple
sevenDaysL = [x for x,y in thisWeekDays]
# get week day numbers 1-7 for this week
for d in sevenDaysL:
w_days_numbers.append(datetime.date(year, mon, d).isocalendar()[2])
# zip week day numbers with the dates
zl = zip(sevenDaysL, w_days_numbers)
# month number prefix
if thisWeekN == 1:
prefix = 'st'
elif thisWeekN == 2:
prefix = 'nd'
elif thisWeekN == 3:
prefix = 'rd'
else:
prefix= "th"
# print heading
print("\n 7 days of this {}{} week: of the year {}"
.format(thisWeekN,prefix, year))
print("-------------------------------------------")
# print results
for el in list(zl):
if el[0] == day:
print("* {} * {}".format(el[0],days[el[1]]))
else:
print(" {} {}".format(el[0],days[el[1]]))
mytime= datetime.datetime.now().strftime("%Y-%m-%d %H-%M")
print("================")
print(mytime)
days_of_the_current_numbered_week()
<强>输出强>
第39周的第7天:2015年
周四17日 周五18日周六19日
周日20日
周一21点22星期二
周三23日答案 2 :(得分:0)
首先,找到年份开始的星期几。这很容易在互联网上找到。这为您提供了一年中第1周的偏移量。
请注意,本周将让您计算一年中的日期,范围为1-365。第01周从第1天开始;第02周从第8天开始,等等。
start_day = 1 +(第1周)* 7 - 偏移
首先,找到年份开始的星期几。这很容易在互联网上找到。这为您提供了一年中第1周的偏移量。
现在你已经开始了,你可以很容易地找到这个月的某一天。在你进入给定月份的范围之前减去每个月的日子是有点乏味的,但这很简单。这会让你前进吗?我已经用英文描述完成了这个,因为你没有给我们任何代码来调试。