您的位置:首页 > 脚本大全 > > 正文

pythondatetime库详解(python使用time、datetime返回工作日列表实例代码)

更多 时间:2021-10-03 01:37:13 类别:脚本大全 浏览量:1046

pythondatetime库详解

python使用time、datetime返回工作日列表实例代码

最近在学习python,动手做了一个自动填写日报的小工具;由于请求中包含时间,格式如:2016-08-04;所以就了解了一下python的时间日期相关函数;这里做简单记录。

函数功能非常简单:获取当月所有工作日(除去周六周天);如果脚本在周六或者周日运行,则添加当天。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • #获取填写日报的日期,规则:把当月所有的工作日时间全部返回
  •   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 datetime time
    您可能感兴趣