2

不知道如何处理这个。

用户提供一个参数,即 program.exe '2001-08-12'

我需要在该参数中添加一天 - 这将代表程序另一部分的日期范围。我知道您可以从当天添加或减去,但是如何从用户提供的日期中添加或减去?

4

3 回答 3

6
import datetime
...
date=time.strptime(argv[1], "%y-%m-%d");
newdate=date + datetime.timedelta(days=1)
于 2012-09-04T16:15:15.773 回答
2

Arnauds 代码是有效的,看看如何使用它:) :-

>>> import datetime
>>> x=datetime.datetime.strptime('2001-08-12','%Y-%m-%d')
>>> newdate=x + datetime.timedelta(days=1)
>>> newdate
datetime.datetime(2001, 8, 13, 0, 0)
>>> 
于 2012-09-04T16:49:44.467 回答
0

好的,这就是我所拥有的:

import sys
from datetime import datetime
user_input = sys.argv[1]                # Get their date string

year_month_day = user_input.split('-')  # Split it into [year, month, day]

year = int(year_month_day[0])
month = int(year_month_day[1])
day = int(year_month_day[2])

date_plus_a_day = datetime(year, month, day+1)

我知道这有点长,但我想确保每个步骤都清楚。如果你想要它更短,我会把它缩短给你。

于 2012-09-04T16:15:56.087 回答