python 计算一年内的所有周的具体日期
2019独角兽企业重金招聘Python工程师标准>>>
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime
import collections
def get_date_list(starttime='20151101',endtime='20151210'):
'''计算输入的起始日期和结束日期之间的所有日期'''datelist=[]d1 = datetime.datetime(int(starttime[0:4]),int(starttime[4:6]),int(starttime[6:8]))d2 = datetime.datetime(int(endtime[0:4]),int(endtime[4:6]),int(endtime[6:8]))for i in range((d2-d1).days+1):d3=d1+datetime.timedelta(days=i)datelist.append(str(d3.strftime("%Y%m%d")))print datelistreturn datelist
def date_list_fun(starttime='20151101',endtime='20151210'):'''计算输入的起始日期和结束日期之间的所有日期'''_u=datetime.timedelta(days=1)startdate=datetime.datetime.strptime(starttime,'%Y%m%d')enddate=datetime.datetime.strptime(endtime,'%Y%m%d')n=0date_list=[]if startdate<=enddate:# print startdate,enddatewhile 1:_time=startdate+n*_udate_list.append(_time.strftime('%Y%m%d'))n=n+1if _time==enddate:breakprint date_listreturn date_list
def allweeks(year=2015):'''计算一年内所有周的具体日期,从1月1号开始,12.31号结束输出如{1: ['20190101','20190106'],...} 只有六天'''start_date=datetime.datetime.strptime(str(year)+'0101','%Y%m%d')end_date=datetime.datetime.strptime(str(year)+'1231','%Y%m%d')_u=datetime.timedelta(days=1)n=0week_date={}while 1:_time=start_date+n*_uw=str(int(_time.strftime('%W'))+1)week_date.setdefault(w,[]).append(_time.strftime('%Y%m%d'))n=n+1if _time==end_date:breakweek_date_start_end={}for i in week_date:week_date_start_end[i]=[week_date[i][0],week_date[i][-1]]print week_dateprint week_date_start_endreturn week_datedef all_weeks(year=2015):'''计算一年内所有周的具体日期,每周都是7天,可能最后一周到 下年week_date 输出如{1: ['20181231', '20190101', '20190102', '20190103', '20190104', '20190105', '20190106'],...}计算一年内所有周的起始日期week_date_start_end {1: ['20181231','20190106'],...}'''start_date=datetime.datetime.strptime(str(int(year)-1)+'1224','%Y%m%d')end_date=datetime.datetime.strptime(str(int(year)+1)+'0107','%Y%m%d')_u=datetime.timedelta(days=1)n=0week_date={}while 1:_time=start_date+n*_uy,w=_time.isocalendar()[:2]if y==year :week_date.setdefault(w,[]).append(_time.strftime('%Y%m%d'))n=n+1if _time==end_date:breakweek_date_start_end={}for i in week_date:week_date_start_end[i]=[week_date[i][0],week_date[i][-1]]print week_date# print week_date_start_endreturn week_date,week_date_start_end
转载于:https://my.oschina.net/u/1458120/blog/547765
python 计算一年内的所有周的具体日期相关推荐
- python星期计算_python 计算一年内的所有周的具体日期
#!/usr/bin/env python # -*- coding:utf-8 -*- import datetime import collections def get_date_list(st ...
- python时间计算_python 计算一年内的所有周的具体日期详解
#!/usr/bin/env python # -*- coding:utf-8 -*- import datetime import collections def date_list_fun(st ...
- python测验9_荐 测验9: Python计算生态纵览 (第9周)
测验9: Python计算生态纵览 (第9周) 单项选择题 1.以下选项不是Python数据可视化方向第三方库的是: A.Matplotlib B.Seaborn C.Mayavi D.Pyramid ...
- MOOC《Python语言程序设计》(第15次)Python计算生态概览(第九周)
MOOC<Python语言程序设计>(第15次) Python计算生态概览(第九周)21.8.26 文章目录 MOOC<Python语言程序设计>(第15次) Python计算 ...
- Python计算今年第n个周m是几月几号
问题描述:计算指定年份的第n个周m是几月几号,例如2018年第40个周五是几月几号. 思考一下,然后到文末阅读参考代码: 1)在Python中,怎么知道特定的年月日是周几?反过来呢? 2)如果实在没有 ...
- 下列不属于python第三方库的是-测验9: Python计算生态纵览 (第9周)-单选题
1.以下选项不是Python数据分析方向第三方库是: ...
- Python计算本周是第几周
原理: 利用 datetime 的减法,获取两个日期(精确到日)之间的天数,再除以7即可: from datetime import datetime# 起始日期,可自行修改 st_date = da ...
- python123测验9答案_python123 测验9: Python计算生态纵览 (第9周)
1.# 以下选项不是Python数据分析 ...
- python计算前一天,前一周,前一月,前一年时间
import datetime from dateutil.relativedelta import relativedelta# 减去一天 (datetime.datetime.now()- dat ...
最新文章
- ExtAspNet应用技巧(十九) - 日志管理
- Idea开发Java WEB 应用
- oracle数据库自动备份
- Linux系统进程类型有哪些?进程状态有哪几种?常见的进程有哪些?
- 架构探险笔记7-事务管理简介
- element.onclick = fun与element onclick=fun()的区别
- spo2数据集_Arduino 血氧心率模块传感器数据采集
- Linux3种进程,linux内核获取进程的全路径3种方法
- zynq中mgtx应用_【干货分享】ZYNQ常用外设设计 (上)
- [设计模式-行为型]状态模式(State)
- JS 语法糖 0 —— 解构
- URL编码 java
- 微信内置浏览器不支持下载文件的解决方案
- 大华相机RTSP获取视频方式
- 1~3年产品经理经典面试题
- 在esp32开发板上实现的web_radio,基于wm8978 codec芯片
- 服务器收到syn包不回消息,TCP:SYN请求接收SYN响应代替SYN-ACK
- ubuntu / linux 禁止鼠标中键的粘贴功能!
- eclipse背景设置什么颜色缓解眼睛疲劳之一
- 光伏发电沦为白菜价 看光伏大佬们如何“割肉”