2Python日历计数

2Python日历计数,python,calendar,Python,Calendar,我的第一天是2012/01/01,我希望我能有一个函数get_daycount,其中: 如何使用python日历模块实现它?IIUC,这就是您想要的 def get_day(x): d=datetime.strptime('2012/01/01' , '%Y/%m/%d') a=(d+timedelta(days=x)).strftime('%Y/%m/%d') return a get_day(5) >>>2012/01/06 使用datetime模

我的第一天是2012/01/01,我希望我能有一个函数get_daycount,其中:


如何使用python日历模块实现它?

IIUC,这就是您想要的

def get_day(x):
    d=datetime.strptime('2012/01/01' , '%Y/%m/%d')
    a=(d+timedelta(days=x)).strftime('%Y/%m/%d')
    return a
get_day(5)
>>>2012/01/06
使用datetime模块来实现似乎更简单

开始日期和结束日期参数有助于增加日期格式规范的灵活性

import datetime


def get_date(count, start_date, date_format):
    count_date = datetime.datetime.strptime(start_date, date_format) + \
                 datetime.timedelta(days=count)
    return count_date.strftime(date_format)


print(get_date(0, '2012/01/01', '%Y/%m/%d'))
print(get_date(1, '2012/01/01', '%Y/%m/%d'))
print(get_date(2, '2012/01/01', '%Y/%m/%d'))

import datetime


def get_date(count, start_date, date_format):
    count_date = datetime.datetime.strptime(start_date, date_format) + \
                 datetime.timedelta(days=count)
    return count_date.strftime(date_format)


print(get_date(0, '2012/01/01', '%Y/%m/%d'))
print(get_date(1, '2012/01/01', '%Y/%m/%d'))
print(get_date(2, '2012/01/01', '%Y/%m/%d'))

Output:
2012/01/01
2012/01/02
2012/01/03