python一个月_使用Python计算一个月中的工作日数?

1586010002-jmsa.png

I am trying to write a Python script that will calculate how many business days are in the current month. For instance if month = August then businessDays = 22.

Here is my code for discovering the month:

def numToMonth( num ):

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

return str(months[ num - 1 ])

This code works fine, and I could hard code another function to match the month with how many days that month should contain...but this does not help me with business days.

Any help? I'm used to C, C++ so please don't bash my Python "skills".

Edit: I cannot install any extra libraries or modules on my machine, so please post answers using default Python modules. (Python 2.7, datetime etc.) Also, my PC has Windows 7 OS.

解决方案

This is a long-winded way, but at least it works and doesn't require anything other than the standard modules.

import datetime

now = datetime.datetime.now()

holidays = {datetime.date(now.year, 8, 14)} # you can add more here

businessdays = 0

for i in range(1, 32):

try:

thisdate = datetime.date(now.year, now.month, i)

except(ValueError):

break

if thisdate.weekday() < 5 and thisdate not in holidays: # Monday == 0, Sunday == 6

businessdays += 1

print businessdays

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值