python 如何获取当月的所有天数?

cbjzeqam  于 2023-10-15  发布在  Python
关注(0)|答案(8)|浏览(100)

我想得到所有的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)]

我该如何解决此问题?
请给我你的想法或建议。谢谢你,谢谢

von4xj4u

von4xj4u1#

下面是一个使用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)]
irlmq6kh

irlmq6kh2#

查看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]]
s1ag04yj

s1ag04yj3#

有几种方法,但使用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
new9mtju

new9mtju4#

使用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
plicqrtu

plicqrtu5#

>>> 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

pqwbnv8z

pqwbnv8z6#

以防万一:

import pandas as pd

print(pd.date_range(start = '2019-4-1',end='2019-4-30', freq='D'))

我们得到:

DatetimeIndex(['2019-04-01', '2019-04-02', '2019-04-03', '2019-04-04',
           '2019-04-05', '2019-04-06', '2019-04-07', '2019-04-08',
           '2019-04-09', '2019-04-10', '2019-04-11', '2019-04-12',
           '2019-04-13', '2019-04-14', '2019-04-15', '2019-04-16',
           '2019-04-17', '2019-04-18', '2019-04-19', '2019-04-20',
           '2019-04-21', '2019-04-22', '2019-04-23', '2019-04-24',
           '2019-04-25', '2019-04-26', '2019-04-27', '2019-04-28',
           '2019-04-29', '2019-04-30'],
          dtype='datetime64[ns]', freq='D')
qjp7pelc

qjp7pelc7#

偶然发现了这一点,并希望随身携带沿着时区。

def get_month_days(date: datetime):
    counter = datetime(date.year, date.month, 1, tzinfo=date.tzinfo)
    date_list = []
    while date.month == counter.month:
        date_list.append(counter)
        counter += timedelta(days=1)
    return date_list
koaltpgm

koaltpgm8#

使用此解决方案,您将能够获得当前月份中每一天的所有 datetime 对象。

import datetime, calendar

today = datetime.datetime.now()
month = today.month
year = today.year

days_per_month = calendar.monthrange(year, month)[1]
days_list = [datetime.date(year, month, day) for day in range(1, days_per_month + 1)]

相关问题