Python123题库完美答案——获得昨天和明天的日期

  • 结果正确-100%
  • 代码规范-100%

题目链接(请点击)

import datetime
import sys


def next_day(date_str):
    date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
    date += datetime.timedelta(days=1)
    return date.strftime('%Y-%m-%d')


def prev_day(date_str):
    date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
    date += datetime.timedelta(days=-1)
    return date.strftime('%Y-%m-%d')


while True:
    line = sys.stdin.readline()
    line = line.strip()
    if line == '':
        break
    print('前一天:', prev_day(line))
    print('后一天:', next_day(line))

你可能感兴趣的:(Python123,python)