2Python日历计数
2Python calendar count
我的第一天是2012/01/01
,希望能有一个函数get_day(count)
,其中:
get_day(0) returns 2012/01/01
get_day(1) returns 2012/01/02
get_day(2) returns 2012/01/03
...
如何使用 python calendar
模块实现它?
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模块来实现(用这个好像更简单)。
start_date
和 end_date
参数有助于增加日期格式规范的灵活性。
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
我的第一天是2012/01/01
,希望能有一个函数get_day(count)
,其中:
get_day(0) returns 2012/01/01
get_day(1) returns 2012/01/02
get_day(2) returns 2012/01/03
...
如何使用 python calendar
模块实现它?
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模块来实现(用这个好像更简单)。
start_date
和 end_date
参数有助于增加日期格式规范的灵活性。
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