题目:
编写一个函数,接收一个日期输入,并输出该日期前一天和后一天的日期 日期格式为:2018-03-21
import datetime import sys def next_day(date_str): date = datetime.datetime.strptime(date_str,'%Y-%m-%d') # 字符串转日期 nextday = date + datetime.timedelta(days = 1) result = nextday.strftime('%Y-%m-%d') # 日期转字符串 return result def prev_day(date_str): date = datetime.datetime.strptime(date_str,'%Y-%m-%d') prevday = date + datetime.timedelta(days = -1) result = prevday.strftime('%Y-%m-%d') return result while True: line = sys.stdin.readline() line = line.strip() if line == '': break print('前一天:', prev_day(line)) print('后一天:', next_day(line))