博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获得昨天和明天的日期
阅读量:811 次
发布时间:2019-03-26

本文共 763 字,大约阅读时间需要 2 分钟。

编写一个函数,接收一个日期输入,并输出该日期前一天和后一天的日期‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬
日期格式为:2018-03-21
input:
2018-01-23
1998-01-01
2018-12-31

output:

前一天: 2018-01-22
后一天: 2018-01-24
前一天: 1997-12-31
后一天: 1998-01-02
前一天: 2018-12-30
后一天: 2019-01-01

import sysfrom datetime import datetime,timedeltadef next_1(x):    start = datetime.strptime(x,'%Y-%m-%d')    next_2 = timedelta(days = 1)    return (start + next_2).strftime('%Y-%m-%d')def prev_1(x):    start_2 = datetime.strptime(x,'%Y-%m-%d')    next_2 = timedelta(days = -1)    return (start_2 + next_2).strftime('%Y-%m-%d')while True:    line = (sys.stdin.readline()).strip()    if line == '':        break    print('前一天:', prev_1(line))    print('后一天:', next_1(line))

转载地址:http://qphyk.baihongyu.com/

你可能感兴趣的文章