python 写出一年中每周的起始日期和终止日期
如果一月一号不在周一, 也认为该周为本年度第一周 而不是 年度的第一个周一为第一周#!/usr/bin/env python# _#_ coding:utf-8 _*_"""#获取一年中每个星期的起始时间#author:yqj@fccs.com"""import datetimeimport timecurrent = datetime.datetime.now()
·
如果一月一号不在周一, 也认为该周为本年度第一周 而不是 年度的第一个周一为第一周
#!/usr/bin/env python
# _#_ coding:utf-8 _*_
"""
#获取一年中每个星期的起始时间
#author:yqj@fccs.com
"""
import datetime
import time
current = datetime.datetime.now()
start = datetime.date(current.year,1,1)
last_day = datetime.date(current.year,12,31)
isfirst = start.weekday()
last_week = last_day.strftime('%W')
weeks={}
if isfirst !=0:
end = datetime.timedelta(7-start.weekday()-1)
weeks[0]=[start,start+end]
start += datetime.timedelta(7 - start.weekday())
def print_date(i):
days = datetime.timedelta(weeks=i)
end = start + days #每周的开始时间
if i+1 == int(last_week):
weeks[i+1] = [end,last_day]
else:
weeks[i+1] = [end,end + datetime.timedelta(6)]
def allweeks():
for i in range(0, int(last_week)):
print_date(i)
return weeks
def main():
allweeks()
for (k,week) in weeks.items():
num = k+1<=9 and "0"+str(k+1) or str(k+1)
print "%s\t%s\t%s\t" %(str(current.year)+num,str(week[0]),str(week[1]))
if __name__ == "__main__":
main()
更多推荐
已为社区贡献2条内容
所有评论(0)