python使用time、datetime返回工作日列表实例代码
作者:土木君 时间:2022-03-21 14:02:15
最近在学习python,动手做了一个自动填写日报的小工具;由于请求中包含时间,格式如:2016-08-04;所以就了解了一下python的时间日期相关函数;这里做简单记录。
函数功能非常简单:获取当月所有工作日(除去周六周天);如果脚本在周六或者周日运行,则添加当天。
#获取填写日报的日期,规则:把当月所有的工作日时间全部返回
def getDateByTime(self):
self.myDate=[]
t = str(time.strftime('%Y-%m-'))
for i in range(1,32):
timeStr=t+str(i)
try:
#字符串转换为规定格式的时间
tmp = time.strptime(timeStr,'%Y-%m-%d')
#判断是否为周六、周日
if (tmp.tm_wday !=6) and (tmp.tm_wday!=5):
self.myDate.append(time.strftime('%Y-%m-%d',tmp))
except:
print('日期越界')
if len(self.myDate)==0:
self.myDate.append(time.strftime('%Y-%m-%d'))
return self.myDate
def getDateByDateTime(self):
self.myDate=[]
now = datetime.datetime.now()
tmp = now.strftime('%Y-%m-')
#通过calendar获取到当月第一天的weekday,以及当月天数
t = calendar.monthrange(now.year, now.month)
for i in range(1,t[1]):
dateTmp = tmp+str(i)
myDateTmp = datetime.datetime.strptime(dateTmp,'%Y-%m-%d')
if myDateTmp.isoweekday() !=6 and myDateTmp.isoweekday() !=7:
self.myDate.append(myDateTmp.strftime('%Y-%m-%d'))
if len(self.myDate)==0:
self.myDate.append(now.strftime('%Y-%m-%d'))
return self.myDate
以上所述是小编给大家介绍的python使用time、datetime返回工作日列表详解整合网站的支持!
来源:https://blog.csdn.net/duyisen/article/details/52119223
标签:python,time,datetime
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python3日期与时间戳转换的几种方法详解
2021-03-10 03:57:17
Php header()函数语法及使用代码
2023-09-04 13:32:20
Qt Quick QML-500行代码实现合成大西瓜游戏
2023-09-22 22:13:07
![](https://img.aspxhome.com/file/2023/9/94119_0s.png)
Python3通过Luhn算法快速验证信用卡卡号的方法
2022-05-29 12:54:58
Python编程基础之类和对象
2023-08-04 11:52:33
![](https://img.aspxhome.com/file/2023/7/61437_0s.jpg)
源码解析python的内存回收机制
2023-05-19 18:12:16
Python实现构建一个仪表板的示例代码
2023-10-03 11:53:27
![](https://img.aspxhome.com/file/2023/8/62258_0s.gif)
求英文字母或数字或下划线或汉字的js正则表达式
2009-08-04 17:59:00
Python程序退出方式小结
2021-12-21 19:29:55
![](https://img.aspxhome.com/file/2023/6/79486_0s.jpg)
Python Beautiful Soup模块使用教程详解
2021-07-21 16:00:10
![](https://img.aspxhome.com/file/2023/9/117889_0s.png)
python b站视频下载的五种版本
2022-08-18 05:28:25
pyecharts调整图例与各板块的位置间距实例
2023-05-15 20:05:40
![](https://img.aspxhome.com/file/2023/2/76762_0s.jpg)
python3.7中安装paddleocr及paddlepaddle包的多种方法
2022-08-11 00:00:57
![](https://img.aspxhome.com/file/2023/5/70775_0s.png)
Python实现Excel自动分组合并单元格
2021-11-06 03:03:41
![](https://img.aspxhome.com/file/2023/5/87915_0s.jpg)
如何让对方在线查看我的程序代码?
2010-05-13 16:34:00
用Python编写分析Python程序性能的工具的教程
2022-02-13 01:57:03
![](https://img.aspxhome.com/file/2023/3/63553_0s.jpg)
python将每个单词按空格分开并保存到文件中
2023-07-01 12:46:31
![](https://img.aspxhome.com/file/2023/9/60309_0s.png)
asp.net中不能在DropDownList中选择多个项 原因分析及解决方法
2023-07-23 22:15:27
MySQL内建复制功能来优化可用性
2010-10-25 20:20:00
Python中的默认参数实例分析
2023-02-20 20:19:38