问题描述:

Not sure how to approach this one.

User supplies an argument, ie, program.exe '2001-08-12'

I need to add a single day to that argument - this will represent a date range for another part of the program. I am aware that you can add or subtract from the current day but how does one add or subtract from a user supplied date?

网友答案:
import datetime
...
date=time.strptime(argv[1], "%y-%m-%d");
newdate=date + datetime.timedelta(days=1)
网友答案:

Arnauds Code is valid,Just see how to use it :) :-

>>> 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)
>>> 
网友答案:

Okay, here's what I've got:

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)

I understand this is a little long, but I wanted to make sure each step was clear. I'll leave shortening it up to you if you want it shorter.

相关阅读:
Top