Skip to content

Instantly share code, notes, and snippets.

@shiro01
Last active July 24, 2018 01:36
Show Gist options
  • Save shiro01/32eebc2a0945d81719bb70343da328e9 to your computer and use it in GitHub Desktop.
Save shiro01/32eebc2a0945d81719bb70343da328e9 to your computer and use it in GitHub Desktop.
日付リストを作成する。
import datetime
from dateutil.relativedelta import relativedelta
def build_target_date_list():
start_date = '2017/01/01'.split('/')
target_date = datetime.date(
int(start_date[0]),
int(start_date[1]),
int(start_date[2])
)
end_date = '2017/02/01'.split('/')
target_end_date = datetime.date(
int(end_date[0]),
int(end_date[1]),
int(end_date[2])
)
print(target_date)
print(target_end_date)
target_date_list = []
while True:
target_date_list.append(
{
"year":target_date.strftime("%Y"),
"month":target_date.strftime("%m"),
"day":target_date.strftime("%d")
}
)
# 次の日
target_date = target_date + relativedelta(days=1)
if (target_date >= target_end_date):
break
print(len(target_date_list))
return target_date_list
test = build_target_date_list()
print(test)
# 結果
# 2017-01-01
# 2017-02-01
# 31
# [
# {'year': '2017', 'month': '01', 'day': '01'},
# {'year': '2017', 'month': '01', 'day': '02'},
# {'year': '2017', 'month': '01', 'day': '03'},
# {'year': '2017', 'month': '01', 'day': '04'},
# {'year': '2017', 'month': '01', 'day': '05'},
# {'year': '2017', 'month': '01', 'day': '06'},
# {'year': '2017', 'month': '01', 'day': '07'},
# {'year': '2017', 'month': '01', 'day': '08'},
# {'year': '2017', 'month': '01', 'day': '09'},
# {'year': '2017', 'month': '01', 'day': '10'},
# {'year': '2017', 'month': '01', 'day': '11'},
# {'year': '2017', 'month': '01', 'day': '12'},
# {'year': '2017', 'month': '01', 'day': '13'},
# {'year': '2017', 'month': '01', 'day': '14'},
# {'year': '2017', 'month': '01', 'day': '15'},
# {'year': '2017', 'month': '01', 'day': '16'},
# {'year': '2017', 'month': '01', 'day': '17'},
# {'year': '2017', 'month': '01', 'day': '18'},
# {'year': '2017', 'month': '01', 'day': '19'},
# {'year': '2017', 'month': '01', 'day': '20'},
# {'year': '2017', 'month': '01', 'day': '21'},
# {'year': '2017', 'month': '01', 'day': '22'},
# {'year': '2017', 'month': '01', 'day': '23'},
# {'year': '2017', 'month': '01', 'day': '24'},
# {'year': '2017', 'month': '01', 'day': '25'},
# {'year': '2017', 'month': '01', 'day': '26'},
# {'year': '2017', 'month': '01', 'day': '27'},
# {'year': '2017', 'month': '01', 'day': '28'},
# {'year': '2017', 'month': '01', 'day': '29'},
# {'year': '2017', 'month': '01', 'day': '30'},
# {'year': '2017', 'month': '01', 'day': '31'}
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment