从python中的yeardatescalendar中提取日期时间信息

时间:2016-03-03 08:55:04

标签: python datetime

import calendar
calendar.Calendar().yeardatescalendar(2014)

>>> [[[[datetime.date(2013, 12, 30), datetime.date(2013, 12, 31),...

上述代码返回2014日历年的日期时间。但是,它还包括2013年的最后2天和2015年的第一天。有什么方法可以提取2014年的信息吗?

5 个答案:

答案 0 :(得分:2)

一种方法:

import datetime
def myFun(year):
     res = []
     d = datetime.datetime(year, 1, 1)
     while d.year != year +1:
             res.append(d)
             d = d + datetime.timedelta(days=1)
     return res

>>> myFun(2014)[:2]
[datetime.datetime(2014, 1, 1, 0, 0), datetime.datetime(2014, 1, 2, 0, 0)]
>>> len(myFun(2014))
365

答案 1 :(得分:2)

您可以找到第一周和上周,并删除不包含年2014的日期时间:

import calendar

c = calendar.Calendar().yeardatescalendar(2014)
#print c

#first week
print c[0][0][0]
[datetime.date(2013, 12, 30), datetime.date(2013, 12, 31), 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)]

#last week
print c[-1][-1][-1]
[datetime.date(2014, 12, 29), datetime.date(2014, 12, 30), datetime.date(2014, 12, 31), datetime.date(2015, 1, 1), datetime.date(2015, 1, 2), datetime.date(2015, 1, 3), datetime.date(2015, 1, 4)]

#filter datetimes by list comprehension
print [x for x in c[0][0][0] if x.year == 2014]
[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)]

print [x for x in c[-1][-1][-1] if x.year == 2014]
[datetime.date(2014, 12, 29), datetime.date(2014, 12, 30), datetime.date(2014, 12, 31)]

#replace first and last week
c[0][0][0] = [x for x in c[0][0][0] if x.year == 2014]
c[-1][-1][-1] = [x for x in c[-1][-1][-1] if x.year == 2014]

print c[0][0][0]
[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)]

print c[-1][-1][-1]
[datetime.date(2014, 12, 29), datetime.date(2014, 12, 30), datetime.date(2014, 12, 31)]

答案 2 :(得分:1)

老实说,这个模块很奇怪。因为看起来2014年有434天。但是,如果您对2014年感兴趣,可以使用nested list comprehension

import calendar
year = calendar.Calendar().yeardatescalendar(2014)
my_year = [day for quarter in year for month in quarter for week in month for day in week if day.year == 2014]

如果您想要没有重复项,可以使用

no_dupe_year = set([day for quarter in year for month in quarter for week in month for day in week if day.year == 2014])
len(no_dupe_year)
>>> 365

答案 3 :(得分:0)

一种简单但粗暴的做法:

c = calendar.Calendar().yeardatescalendar(2014)
for y in c:
    for m in y:
        for w in m:
            for d in reversed(w):
                if d.year != 2014:
                    w.remove(d)

答案 4 :(得分:0)

作为替代方法,您可以自行构建日历,如下所示:

from datetime import datetime, timedelta
from itertools import groupby

def get_calendar(year):
    cur_day = datetime(year, 1, 1)
    one_day = timedelta(days=1)
    days = []

    while cur_day.year == year:
        days.append(cur_day)
        cur_day += one_day

    return [list(g) for k, g in groupby(days, lambda x: x.month)]

for month in get_calendar(2014):    
    print month

这会生成一个月份列表,每个月份都包含datetime格式的天数列表。