如何获得当月的所有日子?

时间:2014-01-20 10:41:32

标签: python datetime

我希望在当月获得每天的所有datetime对象,但我还没有完成。我希望得到的结果是:

[datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), 
 datetime.date(2014, 1, 3), ..., datetime.date(2014, 1, 29),
 datetime.date(2014, 1, 30), datetime.date(2014, 1, 31)]

我该如何解决这个问题?

请告诉我您的想法或建议。谢谢!

6 个答案:

答案 0 :(得分:27)

以下是datetimecalendar的解决方案:

>>> import datetime, calendar
>>> year = 2014
>>> month = 1
>>> num_days = calendar.monthrange(year, month)[1]
>>> days = [datetime.date(year, month, day) for day in range(1, num_days+1)]
>>> days
[datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), datetime.date(2014, 1, 3), datetime.date(2014, 1, 4), datetime.date(2014, 1, 5), datetime.date(2014, 1, 6), datetime.date(2014, 1, 7), datetime.date(2014, 1, 8), datetime.date(2014, 1, 9), datetime.date(2014, 1, 10), datetime.date(2014, 1, 11), datetime.date(2014, 1, 12), datetime.date(2014, 1, 13), datetime.date(2014, 1, 14), datetime.date(2014, 1, 15), datetime.date(2014, 1, 16), datetime.date(2014, 1, 17), datetime.date(2014, 1, 18), datetime.date(2014, 1, 19), datetime.date(2014, 1, 20), datetime.date(2014, 1, 21), datetime.date(2014, 1, 22), datetime.date(2014, 1, 23), datetime.date(2014, 1, 24), datetime.date(2014, 1, 25), datetime.date(2014, 1, 26), datetime.date(2014, 1, 27), datetime.date(2014, 1, 28), datetime.date(2014, 1, 29), datetime.date(2014, 1, 30), datetime.date(2014, 1, 31)]

答案 1 :(得分:6)

查看calendar模块:

import calendar
print calendar.monthcalendar(2013, 4)
[[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, 0, 0, 0, 0, 0]]

答案 2 :(得分:3)

有几种方法,但使用calendar.monthrange,您可以获得一个元组(first_weekday, nb_days)

>>> from calendar import monthrange
>>> monthrange(2014, 2)
(5, 28)
>>> a = monthrange(2014, 2)
>>> range(1, a[1]+1)
[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]
>>> [datetime.date(2014, 2, day) for day in range(1, a[1]+1)]
[datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), datetime.date(2014, 1, 3), datetime.date(2014, 1, 4), datetime.date(2014, 1, 5), datetime.date(2014, 1, 6), datetime.date(2014, 1, 7), datetime.date(2014, 1, 8), datetime.date(2014, 1, 9), datetime.date(2014, 1, 10), datetime.date(2014, 1, 11), datetime.date(2014, 1, 12), datetime.date(2014, 1, 13), datetime.date(2014, 1, 14), datetime.date(2014, 1, 15), datetime.date(2014, 1, 16), datetime.date(2014, 1, 17), datetime.date(2014, 1, 18), datetime.date(2014, 1, 19), datetime.date(2014, 1, 20), datetime.date(2014, 1, 21), datetime.date(2014, 1, 22), datetime.date(2014, 1, 23), datetime.date(2014, 1, 24), datetime.date(2014, 1, 25), datetime.date(2014, 1, 26), datetime.date(2014, 1, 27), datetime.date(2014, 1, 28), datetime.date(2014, 1, 29), datetime.date(2014, 1, 30), datetime.date(2014, 1, 31)]

具有使其更清洁的功能:

from calendar import monthrange
import datetime

def get_datetime_range(year, month):
    nb_days = monthrange(year, month)[1]

    return [datetime.date(year, month, day) for day in range(1, nb_days+1)]

print get_datetime_range(2014, 2)  # usage example

答案 3 :(得分:3)

使用datetimetimedelta

import datetime
d0 = datetime.datetime(year=2014, month=1, day=1)
d1 = datetime.datetime(year=2014, month=2, day=1)
print (d1 - d0).days
>>> 31

答案 4 :(得分:2)

>>> from calendar import Calendar
>>> Calendar().monthdayscalendar(2014,1)
[[0, 0, 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, 0, 0]]

>>> month = Calendar().itermonthdates(2014,1)
>>> print [day for day in month if day.month == 1] # this function will pad to make up full weeks, so filter it by month
[datetime.date(2014, 1, 1), datetime.date(2014, 1, 2), datetime.date(2014, 1, 3), datetime.date(2014, 1, 4), datetime.date(2014, 1, 5), datetime.date(2014, 1, 6), datetime.date(2014, 1, 7), datetime.date(2014, 1, 8), datetime.date(2014, 1, 9), datetime.date(2014, 1, 10), datetime.date(2014, 1, 11), datetime.date(2014, 1, 12), datetime.date(2014, 1, 13), datetime.date(2014, 1, 14), datetime.date(2014, 1, 15), datetime.date(2014, 1, 16), datetime.date(2014, 1, 17), datetime.date(2014, 1, 18), datetime.date(2014, 1, 19), datetime.date(2014, 1, 20), datetime.date(2014, 1, 21), datetime.date(2014, 1, 22), datetime.date(2014, 1, 23), datetime.date(2014, 1, 24), datetime.date(2014, 1, 25), datetime.date(2014, 1, 26), datetime.date(2014, 1, 27), datetime.date(2014, 1, 28), datetime.date(2014, 1, 29), datetime.date(2014, 1, 30), datetime.date(2014, 1, 31)]

http://docs.python.org/3/library/calendar.html

答案 5 :(得分:0)

如果使用pandas

,则可以一行完成
>>> import pandas
>>> import datetime
>>> (datetime.datetime.now() - (datetime.datetime.now() - pandas.DateOffset(months=1))).days
30