在python中查找一个月的最后一个工作日 [英] finding last business day of a month in python

查看:84
本文介绍了在python中查找一个月的最后一个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要查找该月的最后一个工作日。我为此编写了以下代码,并且工作正常,但我想知道是否有更清洁的方法?

I'm trying to find last business day of of the month. I wrote the code below for that and it works fine but I was wondering if there is a cleaner way of doing it?

from  datetime import date,timedelta
import datetime
import calendar

today=datetime.date.today()

last = today.replace(day=calendar.monthrange(today.year,today.month)[1])

if last.weekday()<5:
    print last

else:
    print last-timedelta(days=1+last.weekday()-5)

谢谢!

推荐答案

我使用以下命令:

from pandas.tseries.offsets import BMonthEnd
from datetime import date

d=date.today()

offset = BMonthEnd()

#Last day of current month
offset.rollforward(d)

#Last day of previous month
offset.rollback(d)

这篇关于在python中查找一个月的最后一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆