有没有一种方法可以使用Python的标准库轻松确定(即一个函数调用)给定月份的最后一天?
如果标准库不支持,dateutil包是否支持此功能?
有没有一种方法可以使用Python的标准库轻松确定(即一个函数调用)给定月份的最后一天?
如果标准库不支持,dateutil包是否支持此功能?
当前回答
另一个解决方案是这样做:
from datetime import datetime
def last_day_of_month(year, month):
""" Work out the last day of the month """
last_days = [31, 30, 29, 28, 27]
for i in last_days:
try:
end = datetime(year, month, i)
except ValueError:
continue
else:
return end.date()
return None
使用如下函数:
>>>
>>> last_day_of_month(2008, 2)
datetime.date(2008, 2, 29)
>>> last_day_of_month(2009, 2)
datetime.date(2009, 2, 28)
>>> last_day_of_month(2008, 11)
datetime.date(2008, 11, 30)
>>> last_day_of_month(2008, 12)
datetime.date(2008, 12, 31)
其他回答
import datetime
now = datetime.datetime.now()
start_month = datetime.datetime(now.year, now.month, 1)
date_on_next_month = start_month + datetime.timedelta(35)
start_next_month = datetime.datetime(date_on_next_month.year, date_on_next_month.month, 1)
last_day_month = start_next_month - datetime.timedelta(1)
使用dateutil.relativedelta,您可以获得如下月份的最后一个日期:
from dateutil.relativedelta import relativedelta
last_date_of_month = datetime(mydate.year, mydate.month, 1) + relativedelta(months=1, days=-1)
这个想法是在一个月的第一天,使用relativelta提前一个月,然后返回一天,这样你就能得到你想要的一个月最后一天。
在Python 3.7中,有一个未记录的calendar.monthlen(年,月)函数:
>>> calendar.monthlen(2002, 1)
31
>>> calendar.monthlen(2008, 2)
29
>>> calendar.monthlen(2100, 2)
28
它相当于记录的日历。monthrange(年,月)[1]调用。
如果只有今天是一个月的最后一天,而日期并不重要,那么我更喜欢使用下面的条件。
逻辑很简单。如果明天是下个月的第一天,那么今天就是实际月份的最后一天。下面是if-else条件的两个示例。
from datetime import datetime, timedelta
if (datetime.today()+timedelta(days=1)).day == 1:
print("today is the last day of the month")
else:
print("today isn't the last day of the month")
如果时区意识很重要。
from datetime import datetime, timedelta
import pytz
set(pytz.all_timezones_set)
tz = pytz.timezone("Europe/Berlin")
dt = datetime.today().astimezone(tz=tz)
if (dt+timedelta(days=1)).day == 1:
print("today is the last day of the month")
else:
print("today isn't the last day of the month")
这并不能解决主要问题,但一个获取一个月内最后一个工作日的好方法是使用calendar.monthcalendar,它返回一个日期矩阵,将星期一作为第一列,将星期日作为最后一列。
# Some random date.
some_date = datetime.date(2012, 5, 23)
# Get last weekday
last_weekday = np.asarray(calendar.monthcalendar(some_date.year, some_date.month))[:,0:-2].ravel().max()
print last_weekday
31
整个[0:-2]的事情就是把周末的专栏删掉,然后扔掉。月份以外的日期由0表示,因此最大值实际上忽略了这些日期。
numpy.ravel的使用并不是绝对必要的,但我讨厌仅仅依赖于numpy.ndarray.max如果不知道要在哪个轴上计算,就会使数组变平的约定。